@@ -126,6 +126,26 @@ type Client interface {
126126 GetKeyspaceMetaByName (ctx context.Context , keyspaceName string ) (* keyspacepb.KeyspaceMeta , error )
127127 GetKeyspaceMetaByID (ctx context.Context , keyspaceID uint32 ) (* keyspacepb.KeyspaceMeta , error )
128128
129+ /* Affinity group interfaces */
130+
131+ // CreateAffinityGroups creates one or more affinity groups with key ranges.
132+ // The affinityGroups parameter is a map from group ID to a list of key ranges.
133+ CreateAffinityGroups (ctx context.Context , affinityGroups map [string ][]AffinityGroupKeyRange ) (map [string ]* AffinityGroupState , error )
134+ // GetAffinityGroup gets an affinity group by group ID.
135+ GetAffinityGroup (ctx context.Context , groupID string ) (* AffinityGroupState , error )
136+ // GetAllAffinityGroups gets all affinity groups.
137+ GetAllAffinityGroups (ctx context.Context ) (map [string ]* AffinityGroupState , error )
138+ // UpdateAffinityGroupPeers updates the leader and voter stores of an affinity group.
139+ UpdateAffinityGroupPeers (ctx context.Context , groupID string , leaderStoreID uint64 , voterStoreIDs []uint64 ) (* AffinityGroupState , error )
140+ // DeleteAffinityGroup deletes an affinity group by group ID.
141+ DeleteAffinityGroup (ctx context.Context , groupID string , force bool ) error
142+ // BatchDeleteAffinityGroups deletes multiple affinity groups in batch.
143+ BatchDeleteAffinityGroups (ctx context.Context , groupIDs []string , force bool ) error
144+ // AddAffinityGroupKeyRanges adds key ranges to affinity groups.
145+ AddAffinityGroupKeyRanges (ctx context.Context , groupKeyRanges map [string ][]AffinityGroupKeyRange ) (map [string ]* AffinityGroupState , error )
146+ // RemoveAffinityGroupKeyRanges removes key ranges from affinity groups.
147+ RemoveAffinityGroupKeyRanges (ctx context.Context , groupKeyRanges map [string ][]AffinityGroupKeyRange ) (map [string ]* AffinityGroupState , error )
148+
129149 /* Client-related methods */
130150 // WithCallerID sets and returns a new client with the given caller ID.
131151 WithCallerID (string ) Client
@@ -1213,3 +1233,167 @@ func (c *client) DeleteGCSafePoint(ctx context.Context, serviceID string) (strin
12131233 }
12141234 return msg , nil
12151235}
1236+
1237+ // CreateAffinityGroups creates one or more affinity groups with key ranges.
1238+ func (c * client ) CreateAffinityGroups (ctx context.Context , affinityGroups map [string ][]AffinityGroupKeyRange ) (map [string ]* AffinityGroupState , error ) {
1239+ // Construct the request body
1240+ reqGroups := make (map [string ]CreateAffinityGroupInput , len (affinityGroups ))
1241+ for groupID , ranges := range affinityGroups {
1242+ reqGroups [groupID ] = CreateAffinityGroupInput {Ranges : ranges }
1243+ }
1244+ req := CreateAffinityGroupsRequest {AffinityGroups : reqGroups }
1245+
1246+ reqJSON , err := json .Marshal (req )
1247+ if err != nil {
1248+ return nil , errors .Trace (err )
1249+ }
1250+ var resp AffinityGroupsResponse
1251+ err = c .request (ctx , newRequestInfo ().
1252+ WithName ("CreateAffinityGroups" ).
1253+ WithURI (AffinityGroups ).
1254+ WithMethod (http .MethodPost ).
1255+ WithBody (reqJSON ).
1256+ WithResp (& resp ))
1257+ if err != nil {
1258+ return nil , err
1259+ }
1260+ return resp .AffinityGroups , nil
1261+ }
1262+
1263+ // GetAffinityGroup gets an affinity group by group ID.
1264+ func (c * client ) GetAffinityGroup (ctx context.Context , groupID string ) (* AffinityGroupState , error ) {
1265+ var state AffinityGroupState
1266+ err := c .request (ctx , newRequestInfo ().
1267+ WithName ("GetAffinityGroup" ).
1268+ WithURI (fmt .Sprintf (AffinityGroupByID , groupID )).
1269+ WithMethod (http .MethodGet ).
1270+ WithResp (& state ))
1271+ if err != nil {
1272+ return nil , err
1273+ }
1274+ return & state , nil
1275+ }
1276+
1277+ // GetAllAffinityGroups gets all affinity groups.
1278+ func (c * client ) GetAllAffinityGroups (ctx context.Context ) (map [string ]* AffinityGroupState , error ) {
1279+ var resp AffinityGroupsResponse
1280+ err := c .request (ctx , newRequestInfo ().
1281+ WithName ("GetAllAffinityGroups" ).
1282+ WithURI (AffinityGroups ).
1283+ WithMethod (http .MethodGet ).
1284+ WithResp (& resp ))
1285+ if err != nil {
1286+ return nil , err
1287+ }
1288+ return resp .AffinityGroups , nil
1289+ }
1290+
1291+ // UpdateAffinityGroupPeers updates the leader and voter stores of an affinity group.
1292+ func (c * client ) UpdateAffinityGroupPeers (ctx context.Context , groupID string , leaderStoreID uint64 , voterStoreIDs []uint64 ) (* AffinityGroupState , error ) {
1293+ req := UpdateAffinityGroupPeersRequest {
1294+ LeaderStoreID : leaderStoreID ,
1295+ VoterStoreIDs : voterStoreIDs ,
1296+ }
1297+ reqJSON , err := json .Marshal (req )
1298+ if err != nil {
1299+ return nil , errors .Trace (err )
1300+ }
1301+ var state AffinityGroupState
1302+ err = c .request (ctx , newRequestInfo ().
1303+ WithName ("UpdateAffinityGroupPeers" ).
1304+ WithURI (fmt .Sprintf (AffinityGroupByID , groupID )).
1305+ WithMethod (http .MethodPut ).
1306+ WithBody (reqJSON ).
1307+ WithResp (& state ))
1308+ if err != nil {
1309+ return nil , err
1310+ }
1311+ return & state , nil
1312+ }
1313+
1314+ // DeleteAffinityGroup deletes an affinity group by group ID.
1315+ func (c * client ) DeleteAffinityGroup (ctx context.Context , groupID string , force bool ) error {
1316+ uri := fmt .Sprintf (AffinityGroupByID , groupID )
1317+ if force {
1318+ uri = fmt .Sprintf ("%s?force=true" , uri )
1319+ }
1320+ return c .request (ctx , newRequestInfo ().
1321+ WithName ("DeleteAffinityGroup" ).
1322+ WithURI (uri ).
1323+ WithMethod (http .MethodDelete ))
1324+ }
1325+
1326+ // BatchDeleteAffinityGroups deletes multiple affinity groups in batch.
1327+ func (c * client ) BatchDeleteAffinityGroups (ctx context.Context , groupIDs []string , force bool ) error {
1328+ req := BatchDeleteAffinityGroupsRequest {
1329+ IDs : groupIDs ,
1330+ Force : force ,
1331+ }
1332+ reqJSON , err := json .Marshal (req )
1333+ if err != nil {
1334+ return errors .Trace (err )
1335+ }
1336+ return c .request (ctx , newRequestInfo ().
1337+ WithName ("BatchDeleteAffinityGroups" ).
1338+ WithURI (AffinityGroupBatchDelete ).
1339+ WithMethod (http .MethodPost ).
1340+ WithBody (reqJSON ))
1341+ }
1342+
1343+ // AddAffinityGroupKeyRanges adds key ranges to affinity groups.
1344+ func (c * client ) AddAffinityGroupKeyRanges (ctx context.Context , groupKeyRanges map [string ][]AffinityGroupKeyRange ) (map [string ]* AffinityGroupState , error ) {
1345+ // Convert to the request format
1346+ add := make ([]GroupRangesModification , 0 , len (groupKeyRanges ))
1347+ for groupID , ranges := range groupKeyRanges {
1348+ add = append (add , GroupRangesModification {
1349+ ID : groupID ,
1350+ Ranges : ranges ,
1351+ })
1352+ }
1353+ req := BatchModifyAffinityGroupsRequest {Add : add }
1354+
1355+ reqJSON , err := json .Marshal (req )
1356+ if err != nil {
1357+ return nil , errors .Trace (err )
1358+ }
1359+ var resp AffinityGroupsResponse
1360+ err = c .request (ctx , newRequestInfo ().
1361+ WithName ("AddAffinityGroupKeyRanges" ).
1362+ WithURI (AffinityGroups ).
1363+ WithMethod (http .MethodPatch ).
1364+ WithBody (reqJSON ).
1365+ WithResp (& resp ))
1366+ if err != nil {
1367+ return nil , err
1368+ }
1369+ return resp .AffinityGroups , nil
1370+ }
1371+
1372+ // RemoveAffinityGroupKeyRanges removes key ranges from affinity groups.
1373+ func (c * client ) RemoveAffinityGroupKeyRanges (ctx context.Context , groupKeyRanges map [string ][]AffinityGroupKeyRange ) (map [string ]* AffinityGroupState , error ) {
1374+ // Convert to the request format
1375+ remove := make ([]GroupRangesModification , 0 , len (groupKeyRanges ))
1376+ for groupID , ranges := range groupKeyRanges {
1377+ remove = append (remove , GroupRangesModification {
1378+ ID : groupID ,
1379+ Ranges : ranges ,
1380+ })
1381+ }
1382+ req := BatchModifyAffinityGroupsRequest {Remove : remove }
1383+
1384+ reqJSON , err := json .Marshal (req )
1385+ if err != nil {
1386+ return nil , errors .Trace (err )
1387+ }
1388+ var resp AffinityGroupsResponse
1389+ err = c .request (ctx , newRequestInfo ().
1390+ WithName ("RemoveAffinityGroupKeyRanges" ).
1391+ WithURI (AffinityGroups ).
1392+ WithMethod (http .MethodPatch ).
1393+ WithBody (reqJSON ).
1394+ WithResp (& resp ))
1395+ if err != nil {
1396+ return nil , err
1397+ }
1398+ return resp .AffinityGroups , nil
1399+ }
0 commit comments