View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.protobuf;
19  
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.regex.Pattern;
24
25  import org.apache.hadoop.hbase.CellScannable;
26  import org.apache.hadoop.hbase.DoNotRetryIOException;
27  import org.apache.hadoop.hbase.HColumnDescriptor;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.HTableDescriptor;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.client.Action;
35  import org.apache.hadoop.hbase.client.Append;
36  import org.apache.hadoop.hbase.client.Delete;
37  import org.apache.hadoop.hbase.client.Durability;
38  import org.apache.hadoop.hbase.client.Get;
39  import org.apache.hadoop.hbase.client.Increment;
40  import org.apache.hadoop.hbase.client.MasterSwitchType;
41  import org.apache.hadoop.hbase.client.Mutation;
42  import org.apache.hadoop.hbase.client.Put;
43  import org.apache.hadoop.hbase.client.RegionCoprocessorServiceExec;
44  import org.apache.hadoop.hbase.client.Row;
45  import org.apache.hadoop.hbase.client.RowMutations;
46  import org.apache.hadoop.hbase.client.Scan;
47  import org.apache.hadoop.hbase.exceptions.DeserializationException;
48  import org.apache.hadoop.hbase.filter.ByteArrayComparable;
49  import org.apache.hadoop.hbase.protobuf.generated.AccessControlProtos;
50  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CloseRegionRequest;
51  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.CompactRegionRequest;
52  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.FlushRegionRequest;
53  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetOnlineRegionRequest;
54  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetRegionInfoRequest;
55  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetServerInfoRequest;
56  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.GetStoreFileRequest;
57  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.MergeRegionsRequest;
58  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.OpenRegionRequest;
59  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.OpenRegionRequest.RegionOpenInfo;
60  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.RollWALWriterRequest;
61  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.SplitRegionRequest;
62  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.StopServerRequest;
63  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.UpdateFavoredNodesRequest;
64  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.UpdateFavoredNodesRequest.RegionUpdateInfo;
65  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.WarmupRegionRequest;
66  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
67  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.BulkLoadHFileRequest;
68  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.Condition;
69  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.GetRequest;
70  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutateRequest;
71  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto;
72  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto.ColumnValue;
73  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto.ColumnValue.QualifierValue;
74  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto.MutationType;
75  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionAction;
76  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
77  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.CompareType;
78  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier;
79  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType;
80  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos;
81  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.AddColumnRequest;
82  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.AssignRegionRequest;
83  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.BalanceRequest;
84  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.CreateTableRequest;
85  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.DeleteColumnRequest;
86  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.DeleteTableRequest;
87  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.DisableTableRequest;
88  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.DispatchMergingRegionsRequest;
89  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.EnableCatalogJanitorRequest;
90  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.EnableTableRequest;
91  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetClusterStatusRequest;
92  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetSchemaAlterStatusRequest;
93  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetTableDescriptorsRequest;
94  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetTableNamesRequest;
95  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetTableStateRequest;
96  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsBalancerEnabledRequest;
97  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsCatalogJanitorEnabledRequest;
98  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsMasterRunningRequest;
99  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsNormalizerEnabledRequest;
100 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSplitOrMergeEnabledRequest;
101 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.ModifyColumnRequest;
102 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.ModifyTableRequest;
103 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.MoveRegionRequest;
104 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.NormalizeRequest;
105 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.OfflineRegionRequest;
106 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.RunCatalogScanRequest;
107 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.SetBalancerRunningRequest;
108 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.SetNormalizerRunningRequest;
109 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.SetSplitOrMergeEnabledRequest;
110 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.TruncateTableRequest;
111 import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.UnassignRegionRequest;
112 import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.GetLastFlushedSequenceIdRequest;
113 import org.apache.hadoop.hbase.util.ByteStringer;
114 import org.apache.hadoop.hbase.util.Bytes;
115 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
116 import org.apache.hadoop.hbase.util.Pair;
117 import org.apache.hadoop.security.token.Token;
118
119 import com.google.protobuf.ByteString;
120
121 /**
122  * Helper utility to build protocol buffer requests,
123  * or build components for protocol buffer requests.
124  */
125 @InterfaceAudience.Private
126 public final class RequestConverter {
127
128   private RequestConverter() {
129   }
130
131 // Start utilities for Client
132
133   /**
134    * Create a protocol buffer GetRequest for a client Get
135    *
136    * @param regionName the name of the region to get
137    * @param get the client Get
138    * @return a protocol buffer GetRequest
139    */
140   public static GetRequest buildGetRequest(final byte[] regionName,
141       final Get get) throws IOException {
142     GetRequest.Builder builder = GetRequest.newBuilder();
143     RegionSpecifier region = buildRegionSpecifier(
144       RegionSpecifierType.REGION_NAME, regionName);
145     builder.setRegion(region);
146     builder.setGet(ProtobufUtil.toGet(get));
147     return builder.build();
148   }
149
150   /**
151    * Create a protocol buffer MutateRequest for a client increment
152    *
153    * @param regionName
154    * @param row
155    * @param family
156    * @param qualifier
157    * @param amount
158    * @param durability
159    * @return a mutate request
160    */
161   public static MutateRequest buildIncrementRequest(
162       final byte[] regionName, final byte[] row, final byte[] family, final byte[] qualifier,
163       final long amount, final Durability durability, long nonceGroup, long nonce) {
164     MutateRequest.Builder builder = MutateRequest.newBuilder();
165     RegionSpecifier region = buildRegionSpecifier(
166       RegionSpecifierType.REGION_NAME, regionName);
167     builder.setRegion(region);
168
169     MutationProto.Builder mutateBuilder = MutationProto.newBuilder();
170     mutateBuilder.setRow(ByteStringer.wrap(row));
171     mutateBuilder.setMutateType(MutationType.INCREMENT);
172     mutateBuilder.setDurability(ProtobufUtil.toDurability(durability));
173     ColumnValue.Builder columnBuilder = ColumnValue.newBuilder();
174     columnBuilder.setFamily(ByteStringer.wrap(family));
175     QualifierValue.Builder valueBuilder = QualifierValue.newBuilder();
176     valueBuilder.setValue(ByteStringer.wrap(Bytes.toBytes(amount)));
177     valueBuilder.setQualifier(ByteStringer.wrap(qualifier));
178     columnBuilder.addQualifierValue(valueBuilder.build());
179     mutateBuilder.addColumnValue(columnBuilder.build());
180     if (nonce != HConstants.NO_NONCE) {
181       mutateBuilder.setNonce(nonce);
182     }
183     builder.setMutation(mutateBuilder.build());
184     if (nonceGroup != HConstants.NO_NONCE) {
185       builder.setNonceGroup(nonceGroup);
186     }
187     return builder.build();
188   }
189
190   /**
191    * Create a protocol buffer MutateRequest for a conditioned put
192    *
193    * @param regionName
194    * @param row
195    * @param family
196    * @param qualifier
197    * @param comparator
198    * @param compareType
199    * @param put
200    * @return a mutate request
201    * @throws IOException
202    */
203   public static MutateRequest buildMutateRequest(
204       final byte[] regionName, final byte[] row, final byte[] family,
205       final byte [] qualifier, final ByteArrayComparable comparator,
206       final CompareType compareType, final Put put) throws IOException {
207     MutateRequest.Builder builder = MutateRequest.newBuilder();
208     RegionSpecifier region = buildRegionSpecifier(
209       RegionSpecifierType.REGION_NAME, regionName);
210     builder.setRegion(region);
211     Condition condition = buildCondition(
212       row, family, qualifier, comparator, compareType);
213     builder.setMutation(ProtobufUtil.toMutation(MutationType.PUT, put, MutationProto.newBuilder()));
214     builder.setCondition(condition);
215     return builder.build();
216   }
217
218   /**
219    * Create a protocol buffer MutateRequest for a conditioned delete
220    *
221    * @param regionName
222    * @param row
223    * @param family
224    * @param qualifier
225    * @param comparator
226    * @param compareType
227    * @param delete
228    * @return a mutate request
229    * @throws IOException
230    */
231   public static MutateRequest buildMutateRequest(
232       final byte[] regionName, final byte[] row, final byte[] family,
233       final byte [] qualifier, final ByteArrayComparable comparator,
234       final CompareType compareType, final Delete delete) throws IOException {
235     MutateRequest.Builder builder = MutateRequest.newBuilder();
236     RegionSpecifier region = buildRegionSpecifier(
237       RegionSpecifierType.REGION_NAME, regionName);
238     builder.setRegion(region);
239     Condition condition = buildCondition(
240       row, family, qualifier, comparator, compareType);
241     builder.setMutation(ProtobufUtil.toMutation(MutationType.DELETE, delete,
242       MutationProto.newBuilder()));
243     builder.setCondition(condition);
244     return builder.build();
245   }
246
247   /**
248    * Create a protocol buffer MutateRequest for conditioned row mutations
249    *
250    * @param regionName
251    * @param row
252    * @param family
253    * @param qualifier
254    * @param comparator
255    * @param compareType
256    * @param rowMutations
257    * @return a mutate request
258    * @throws IOException
259    */
260   public static ClientProtos.MultiRequest buildMutateRequest(
261       final byte[] regionName, final byte[] row, final byte[] family,
262       final byte [] qualifier, final ByteArrayComparable comparator,
263       final CompareType compareType, final RowMutations rowMutations) throws IOException {
264     RegionAction.Builder builder =
265         getRegionActionBuilderWithRegion(RegionAction.newBuilder(), regionName);
266     builder.setAtomic(true);
267     ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder();
268     MutationProto.Builder mutationBuilder = MutationProto.newBuilder();
269     Condition condition = buildCondition(
270         row, family, qualifier, comparator, compareType);
271     for (Mutation mutation: rowMutations.getMutations()) {
272       MutationType mutateType = null;
273       if (mutation instanceof Put) {
274         mutateType = MutationType.PUT;
275       } else if (mutation instanceof Delete) {
276         mutateType = MutationType.DELETE;
277       } else {
278         throw new DoNotRetryIOException("RowMutations supports only put and delete, not " +
279             mutation.getClass().getName());
280       }
281       mutationBuilder.clear();
282       MutationProto mp = ProtobufUtil.toMutation(mutateType, mutation, mutationBuilder);
283       actionBuilder.clear();
284       actionBuilder.setMutation(mp);
285       builder.addAction(actionBuilder.build());
286     }
287     ClientProtos.MultiRequest request =
288         ClientProtos.MultiRequest.newBuilder().addRegionAction(builder.build())
289             .setCondition(condition).build();
290     return request;
291   }
292
293   /**
294    * Create a protocol buffer MutateRequest for a put
295    *
296    * @param regionName
297    * @param put
298    * @return a mutate request
299    * @throws IOException
300    */
301   public static MutateRequest buildMutateRequest(
302       final byte[] regionName, final Put put) throws IOException {
303     MutateRequest.Builder builder = MutateRequest.newBuilder();
304     RegionSpecifier region = buildRegionSpecifier(
305       RegionSpecifierType.REGION_NAME, regionName);
306     builder.setRegion(region);
307     builder.setMutation(ProtobufUtil.toMutation(MutationType.PUT, put, MutationProto.newBuilder()));
308     return builder.build();
309   }
310
311   /**
312    * Create a protocol buffer MutateRequest for an append
313    *
314    * @param regionName
315    * @param append
316    * @return a mutate request
317    * @throws IOException
318    */
319   public static MutateRequest buildMutateRequest(final byte[] regionName,
320       final Append append, long nonceGroup, long nonce) throws IOException {
321     MutateRequest.Builder builder = MutateRequest.newBuilder();
322     RegionSpecifier region = buildRegionSpecifier(
323       RegionSpecifierType.REGION_NAME, regionName);
324     builder.setRegion(region);
325     if (nonce != HConstants.NO_NONCE && nonceGroup != HConstants.NO_NONCE) {
326       builder.setNonceGroup(nonceGroup);
327     }
328     builder.setMutation(ProtobufUtil.toMutation(MutationType.APPEND, append,
329       MutationProto.newBuilder(), nonce));
330     return builder.build();
331   }
332
333   /**
334    * Create a protocol buffer MutateRequest for a client increment
335    *
336    * @param regionName
337    * @param increment
338    * @return a mutate request
339    */
340   public static MutateRequest buildMutateRequest(final byte[] regionName,
341       final Increment increment, final long nonceGroup, final long nonce) {
342     MutateRequest.Builder builder = MutateRequest.newBuilder();
343     RegionSpecifier region = buildRegionSpecifier(
344       RegionSpecifierType.REGION_NAME, regionName);
345     builder.setRegion(region);
346     if (nonce != HConstants.NO_NONCE && nonceGroup != HConstants.NO_NONCE) {
347       builder.setNonceGroup(nonceGroup);
348     }
349     builder.setMutation(ProtobufUtil.toMutation(increment, MutationProto.newBuilder(), nonce));
350     return builder.build();
351   }
352
353   /**
354    * Create a protocol buffer MutateRequest for a delete
355    *
356    * @param regionName
357    * @param delete
358    * @return a mutate request
359    * @throws IOException
360    */
361   public static MutateRequest buildMutateRequest(
362       final byte[] regionName, final Delete delete) throws IOException {
363     MutateRequest.Builder builder = MutateRequest.newBuilder();
364     RegionSpecifier region = buildRegionSpecifier(
365       RegionSpecifierType.REGION_NAME, regionName);
366     builder.setRegion(region);
367     builder.setMutation(ProtobufUtil.toMutation(MutationType.DELETE, delete,
368       MutationProto.newBuilder()));
369     return builder.build();
370   }
371
372   /**
373    * Create a protocol buffer MultiRequest for row mutations.
374    * Does not propagate Action absolute position.  Does not set atomic action on the created
375    * RegionAtomic.  Caller should do that if wanted.
376    * @param regionName
377    * @param rowMutations
378    * @return a data-laden RegionMutation.Builder
379    * @throws IOException
380    */
381   public static RegionAction.Builder buildRegionAction(final byte [] regionName,
382       final RowMutations rowMutations)
383   throws IOException {
384     RegionAction.Builder builder =
385       getRegionActionBuilderWithRegion(RegionAction.newBuilder(), regionName);
386     ClientProtos.Action.Builder actionBuilder = ClientProtos.Action.newBuilder();
387     MutationProto.Builder mutationBuilder = MutationProto.newBuilder();
388     for (Mutation mutation: rowMutations.getMutations()) {
389       MutationType mutateType = null;
390       if (mutation instanceof Put) {
391         mutateType = MutationType.PUT;
392       } else if (mutation instanceof Delete) {
393         mutateType = MutationType.DELETE;
394       } else {
395         throw new DoNotRetryIOException("RowMutations supports only put and delete, not " +
396           mutation.getClass().getName());
397       }
398       mutationBuilder.clear();
399       MutationProto mp = ProtobufUtil.toMutation(mutateType, mutation, mutationBuilder);
400       actionBuilder.clear();
401       actionBuilder.setMutation(mp);
402       builder.addAction(actionBuilder.build());
403     }
404     return builder;
405   }
406
407   /**
408    * Create a protocol buffer MultiRequest for row mutations that does not hold data.  Data/Cells
409    * are carried outside of protobuf.  Return references to the Cells in <code>cells</code> param.
410     * Does not propagate Action absolute position.  Does not set atomic action on the created
411    * RegionAtomic.  Caller should do that if wanted.
412    * @param regionName
413    * @param rowMutations
414    * @param cells Return in here a list of Cells as CellIterable.
415    * @return a region mutation minus data
416    * @throws IOException
417    */
418   public static RegionAction.Builder buildNoDataRegionAction(final byte[] regionName,
419       final RowMutations rowMutations, final List<CellScannable> cells,
420       final RegionAction.Builder regionActionBuilder,
421       final ClientProtos.Action.Builder actionBuilder,
422       final MutationProto.Builder mutationBuilder)
423   throws IOException {
424     for (Mutation mutation: rowMutations.getMutations()) {
425       MutationType type = null;
426       if (mutation instanceof Put) {
427         type = MutationType.PUT;
428       } else if (mutation instanceof Delete) {
429         type = MutationType.DELETE;
430       } else {
431         throw new DoNotRetryIOException("RowMutations supports only put and delete, not " +
432           mutation.getClass().getName());
433       }
434       mutationBuilder.clear();
435       MutationProto mp = ProtobufUtil.toMutationNoData(type, mutation, mutationBuilder);
436       cells.add(mutation);
437       actionBuilder.clear();
438       regionActionBuilder.addAction(actionBuilder.setMutation(mp).build());
439     }
440     return regionActionBuilder;
441   }
442
443   private static RegionAction.Builder getRegionActionBuilderWithRegion(
444       final RegionAction.Builder regionActionBuilder, final byte [] regionName) {
445     RegionSpecifier region = buildRegionSpecifier(RegionSpecifierType.REGION_NAME, regionName);
446     regionActionBuilder.setRegion(region);
447     return regionActionBuilder;
448   }
449
450   /**
451    * Create a protocol buffer ScanRequest for a client Scan
452    *
453    * @param regionName
454    * @param scan
455    * @param numberOfRows
456    * @param closeScanner
457    * @return a scan request
458    * @throws IOException
459    */
460   public static ScanRequest buildScanRequest(final byte[] regionName, final Scan scan,
461       final int numberOfRows, final boolean closeScanner) throws IOException {
462     ScanRequest.Builder builder = ScanRequest.newBuilder();
463     RegionSpecifier region = buildRegionSpecifier(
464       RegionSpecifierType.REGION_NAME, regionName);
465     builder.setNumberOfRows(numberOfRows);
466     builder.setCloseScanner(closeScanner);
467     builder.setRegion(region);
468     builder.setScan(ProtobufUtil.toScan(scan));
469     builder.setClientHandlesPartials(true);
470     builder.setClientHandlesHeartbeats(true);
471     builder.setTrackScanMetrics(scan.isScanMetricsEnabled());
472     return builder.build();
473   }
474
475   /**
476    * Create a protocol buffer ScanRequest for a scanner id
477    *
478    * @param scannerId
479    * @param numberOfRows
480    * @param closeScanner
481    * @return a scan request
482    */
483   public static ScanRequest buildScanRequest(final long scannerId, final int numberOfRows,
484       final boolean closeScanner, final boolean trackMetrics) {
485     ScanRequest.Builder builder = ScanRequest.newBuilder();
486     builder.setNumberOfRows(numberOfRows);
487     builder.setCloseScanner(closeScanner);
488     builder.setScannerId(scannerId);
489     builder.setClientHandlesPartials(true);
490     builder.setClientHandlesHeartbeats(true);
491     builder.setTrackScanMetrics(trackMetrics);
492     return builder.build();
493   }
494
495   /**
496    * Create a protocol buffer ScanRequest for a scanner id
497    *
498    * @param scannerId
499    * @param numberOfRows
500    * @param closeScanner
501    * @param nextCallSeq
502    * @return a scan request
503    */
504   public static ScanRequest buildScanRequest(final long scannerId, final int numberOfRows,
505       final boolean closeScanner, final long nextCallSeq, final boolean trackMetrics,
506       final boolean renew) {
507     ScanRequest.Builder builder = ScanRequest.newBuilder();
508     builder.setNumberOfRows(numberOfRows);
509     builder.setCloseScanner(closeScanner);
510     builder.setScannerId(scannerId);
511     builder.setNextCallSeq(nextCallSeq);
512     builder.setClientHandlesPartials(true);
513     builder.setClientHandlesHeartbeats(true);
514     builder.setTrackScanMetrics(trackMetrics);
515     builder.setRenew(renew);
516     return builder.build();
517   }
518
519   /**
520    * Create a protocol buffer bulk load request
521    *
522    * @param familyPaths
523    * @param regionName
524    * @param assignSeqNum
525    * @return a bulk load request
526    */
527   public static BulkLoadHFileRequest buildBulkLoadHFileRequest(
528       final List<Pair<byte[], String>> familyPaths,
529       final byte[] regionName, boolean assignSeqNum,
530       final Token<?> userToken, final String bulkToken) {
531     RegionSpecifier region = RequestConverter.buildRegionSpecifier(
532       RegionSpecifierType.REGION_NAME, regionName);
533
534     ClientProtos.DelegationToken protoDT = null;
535     if (userToken != null) {
536       protoDT =
537           ClientProtos.DelegationToken.newBuilder()
538             .setIdentifier(ByteStringer.wrap(userToken.getIdentifier()))
539             .setPassword(ByteStringer.wrap(userToken.getPassword()))
540             .setKind(userToken.getKind().toString())
541             .setService(userToken.getService().toString()).build();
542     }
543
544     List<ClientProtos.BulkLoadHFileRequest.FamilyPath> protoFamilyPaths =
545         new ArrayList<ClientProtos.BulkLoadHFileRequest.FamilyPath>(familyPaths.size());
546     for(Pair<byte[], String> el: familyPaths) {
547       protoFamilyPaths.add(ClientProtos.BulkLoadHFileRequest.FamilyPath.newBuilder()
548         .setFamily(ByteStringer.wrap(el.getFirst()))
549         .setPath(el.getSecond()).build());
550     }
551
552     BulkLoadHFileRequest.Builder request =
553         ClientProtos.BulkLoadHFileRequest.newBuilder()
554           .setRegion(region)
555           .setAssignSeqNum(assignSeqNum)
556           .addAllFamilyPath(protoFamilyPaths);
557     if (userToken != null) {
558       request.setFsToken(protoDT);
559     }
560     if (bulkToken != null) {
561       request.setBulkToken(bulkToken);
562     }
563     return request.build();
564   }
565
566   /**
567    * Create a protocol buffer multi request for a list of actions.
568    * Propagates Actions original index.
569    *
570    * @param regionName
571    * @param actions
572    * @return a multi request
573    * @throws IOException
574    */
575   public static <R> RegionAction.Builder buildRegionAction(final byte[] regionName,
576       final List<Action<R>> actions, final RegionAction.Builder regionActionBuilder,
577       final ClientProtos.Action.Builder actionBuilder,
578       final MutationProto.Builder mutationBuilder) throws IOException {
579     for (Action<R> action: actions) {
580       Row row = action.getAction();
581       actionBuilder.clear();
582       actionBuilder.setIndex(action.getOriginalIndex());
583       mutationBuilder.clear();
584       if (row instanceof Get) {
585         Get g = (Get)row;
586         regionActionBuilder.addAction(actionBuilder.setGet(ProtobufUtil.toGet(g)));
587       } else if (row instanceof Put) {
588         regionActionBuilder.addAction(actionBuilder.
589           setMutation(ProtobufUtil.toMutation(MutationType.PUT, (Put)row, mutationBuilder)));
590       } else if (row instanceof Delete) {
591         regionActionBuilder.addAction(actionBuilder.
592           setMutation(ProtobufUtil.toMutation(MutationType.DELETE, (Delete)row, mutationBuilder)));
593       } else if (row instanceof Append) {
594         regionActionBuilder.addAction(actionBuilder.setMutation(ProtobufUtil.toMutation(
595             MutationType.APPEND, (Append)row, mutationBuilder, action.getNonce())));
596       } else if (row instanceof Increment) {
597         regionActionBuilder.addAction(actionBuilder.setMutation(
598             ProtobufUtil.toMutation((Increment)row, mutationBuilder, action.getNonce())));
599       } else if (row instanceof RegionCoprocessorServiceExec) {
600         RegionCoprocessorServiceExec exec = (RegionCoprocessorServiceExec) row;
601         regionActionBuilder.addAction(actionBuilder.setServiceCall(
602             ClientProtos.CoprocessorServiceCall.newBuilder()
603               .setRow(ByteStringer.wrap(exec.getRow()))
604               .setServiceName(exec.getMethod().getService().getFullName())
605               .setMethodName(exec.getMethod().getName())
606               .setRequest(exec.getRequest().toByteString())));
607       } else if (row instanceof RowMutations) {
608         throw new UnsupportedOperationException("No RowMutations in multi calls; use mutateRow");
609       } else {
610         throw new DoNotRetryIOException("Multi doesn't support " + row.getClass().getName());
611       }
612     }
613     return regionActionBuilder;
614   }
615
616   /**
617    * Create a protocol buffer multirequest with NO data for a list of actions (data is carried
618    * otherwise than via protobuf).  This means it just notes attributes, whether to write the
619    * WAL, etc., and the presence in protobuf serves as place holder for the data which is
620    * coming along otherwise.  Note that Get is different.  It does not contain 'data' and is always
621    * carried by protobuf.  We return references to the data by adding them to the passed in
622    * <code>data</code> param.
623    *
624    * <p>Propagates Actions original index.
625    *
626    * @param regionName
627    * @param actions
628    * @param cells Place to stuff references to actual data.
629    * @return a multi request that does not carry any data.
630    * @throws IOException
631    */
632   public static <R> RegionAction.Builder buildNoDataRegionAction(final byte[] regionName,
633       final List<Action<R>> actions, final List<CellScannable> cells,
634       final RegionAction.Builder regionActionBuilder,
635       final ClientProtos.Action.Builder actionBuilder,
636       final MutationProto.Builder mutationBuilder) throws IOException {
637     RegionAction.Builder builder = getRegionActionBuilderWithRegion(
638       RegionAction.newBuilder(), regionName);
639     for (Action<R> action: actions) {
640       Row row = action.getAction();
641       actionBuilder.clear();
642       actionBuilder.setIndex(action.getOriginalIndex());
643       mutationBuilder.clear();
644       if (row instanceof Get) {
645         Get g = (Get)row;
646         builder.addAction(actionBuilder.setGet(ProtobufUtil.toGet(g)));
647       } else if (row instanceof Put) {
648         Put p = (Put)row;
649         cells.add(p);
650         builder.addAction(actionBuilder.
651           setMutation(ProtobufUtil.toMutationNoData(MutationType.PUT, p, mutationBuilder)));
652       } else if (row instanceof Delete) {
653         Delete d = (Delete)row;
654         int size = d.size();
655         // Note that a legitimate Delete may have a size of zero; i.e. a Delete that has nothing
656         // in it but the row to delete.  In this case, the current implementation does not make
657         // a KeyValue to represent a delete-of-all-the-row until we serialize... For such cases
658         // where the size returned is zero, we will send the Delete fully pb'd rather than have
659         // metadata only in the pb and then send the kv along the side in cells.
660         if (size > 0) {
661           cells.add(d);
662           builder.addAction(actionBuilder.
663             setMutation(ProtobufUtil.toMutationNoData(MutationType.DELETE, d, mutationBuilder)));
664         } else {
665           builder.addAction(actionBuilder.
666             setMutation(ProtobufUtil.toMutation(MutationType.DELETE, d, mutationBuilder)));
667         }
668       } else if (row instanceof Append) {
669         Append a = (Append)row;
670         cells.add(a);
671         builder.addAction(actionBuilder.setMutation(ProtobufUtil.toMutationNoData(
672           MutationType.APPEND, a, mutationBuilder, action.getNonce())));
673       } else if (row instanceof Increment) {
674         Increment i = (Increment)row;
675         cells.add(i);
676         builder.addAction(actionBuilder.setMutation(ProtobufUtil.toMutationNoData(
677           MutationType.INCREMENT, i, mutationBuilder, action.getNonce())));
678       } else if (row instanceof RegionCoprocessorServiceExec) {
679         RegionCoprocessorServiceExec exec = (RegionCoprocessorServiceExec) row;
680         builder.addAction(actionBuilder.setServiceCall(
681             ClientProtos.CoprocessorServiceCall.newBuilder()
682               .setRow(ByteStringer.wrap(exec.getRow()))
683               .setServiceName(exec.getMethod().getService().getFullName())
684               .setMethodName(exec.getMethod().getName())
685               .setRequest(exec.getRequest().toByteString())));
686       } else if (row instanceof RowMutations) {
687         throw new UnsupportedOperationException("No RowMutations in multi calls; use mutateRow");
688       } else {
689         throw new DoNotRetryIOException("Multi doesn't support " + row.getClass().getName());
690       }
691     }
692     return builder;
693   }
694
695 // End utilities for Client
696 //Start utilities for Admin
697
698   /**
699    * Create a protocol buffer GetRegionInfoRequest for a given region name
700    *
701    * @param regionName the name of the region to get info
702    * @return a protocol buffer GetRegionInfoRequest
703    */
704   public static GetRegionInfoRequest
705       buildGetRegionInfoRequest(final byte[] regionName) {
706     return buildGetRegionInfoRequest(regionName, false);
707   }
708
709   /**
710    * Create a protocol buffer GetRegionInfoRequest for a given region name
711    *
712    * @param regionName the name of the region to get info
713    * @param includeCompactionState indicate if the compaction state is requested
714    * @return a protocol buffer GetRegionInfoRequest
715    */
716   public static GetRegionInfoRequest
717       buildGetRegionInfoRequest(final byte[] regionName,
718         final boolean includeCompactionState) {
719     GetRegionInfoRequest.Builder builder = GetRegionInfoRequest.newBuilder();
720     RegionSpecifier region = buildRegionSpecifier(
721       RegionSpecifierType.REGION_NAME, regionName);
722     builder.setRegion(region);
723     if (includeCompactionState) {
724       builder.setCompactionState(includeCompactionState);
725     }
726     return builder.build();
727   }
728
729  /**
730   * Create a protocol buffer GetStoreFileRequest for a given region name
731   *
732   * @param regionName the name of the region to get info
733   * @param family the family to get store file list
734   * @return a protocol buffer GetStoreFileRequest
735   */
736  public static GetStoreFileRequest
737      buildGetStoreFileRequest(final byte[] regionName, final byte[] family) {
738    GetStoreFileRequest.Builder builder = GetStoreFileRequest.newBuilder();
739    RegionSpecifier region = buildRegionSpecifier(
740      RegionSpecifierType.REGION_NAME, regionName);
741    builder.setRegion(region);
742    builder.addFamily(ByteStringer.wrap(family));
743    return builder.build();
744  }
745
746  /**
747   * Create a protocol buffer GetOnlineRegionRequest
748   *
749   * @return a protocol buffer GetOnlineRegionRequest
750   */
751  public static GetOnlineRegionRequest buildGetOnlineRegionRequest() {
752    return GetOnlineRegionRequest.newBuilder().build();
753  }
754
755  /**
756   * Create a protocol buffer FlushRegionRequest for a given region name
757   *
758   * @param regionName the name of the region to get info
759   * @return a protocol buffer FlushRegionRequest
760   */
761  public static FlushRegionRequest
762      buildFlushRegionRequest(final byte[] regionName) {
763    return buildFlushRegionRequest(regionName, false);
764  }
765
766  /**
767   * Create a protocol buffer FlushRegionRequest for a given region name
768   *
769   * @param regionName the name of the region to get info
770   * @return a protocol buffer FlushRegionRequest
771   */
772  public static FlushRegionRequest
773      buildFlushRegionRequest(final byte[] regionName, boolean writeFlushWALMarker) {
774    FlushRegionRequest.Builder builder = FlushRegionRequest.newBuilder();
775    RegionSpecifier region = buildRegionSpecifier(
776      RegionSpecifierType.REGION_NAME, regionName);
777    builder.setRegion(region);
778    builder.setWriteFlushWalMarker(writeFlushWALMarker);
779    return builder.build();
780  }
781
782  /**
783   * Create a protocol buffer OpenRegionRequest to open a list of regions
784   *
785   * @param server the serverName for the RPC
786   * @param regionOpenInfos info of a list of regions to open
787   * @param openForReplay
788   * @return a protocol buffer OpenRegionRequest
789   */
790  public static OpenRegionRequest
791      buildOpenRegionRequest(ServerName server, final List<Pair<HRegionInfo,
792          List<ServerName>>> regionOpenInfos, Boolean openForReplay) {
793    OpenRegionRequest.Builder builder = OpenRegionRequest.newBuilder();
794    for (Pair<HRegionInfo, List<ServerName>> regionOpenInfo: regionOpenInfos) {
795      builder.addOpenInfo(buildRegionOpenInfo(regionOpenInfo.getFirst(),
796        regionOpenInfo.getSecond(), openForReplay));
797    }
798    if (server != null) {
799      builder.setServerStartCode(server.getStartcode());
800    }
801    // send the master's wall clock time as well, so that the RS can refer to it
802    builder.setMasterSystemTime(EnvironmentEdgeManager.currentTime());
803    return builder.build();
804  }
805
806  /**
807   * Create a protocol buffer OpenRegionRequest for a given region
808   *
809   * @param server the serverName for the RPC
810   * @param region the region to open
811   * @param favoredNodes
812   * @param openForReplay
813   * @return a protocol buffer OpenRegionRequest
814   */
815  public static OpenRegionRequest buildOpenRegionRequest(ServerName server,
816      final HRegionInfo region, List<ServerName> favoredNodes,
817      Boolean openForReplay) {
818    OpenRegionRequest.Builder builder = OpenRegionRequest.newBuilder();
819    builder.addOpenInfo(buildRegionOpenInfo(region, favoredNodes,
820      openForReplay));
821    if (server != null) {
822      builder.setServerStartCode(server.getStartcode());
823    }
824    builder.setMasterSystemTime(EnvironmentEdgeManager.currentTime());
825    return builder.build();
826  }
827
828  /**
829   * Create a protocol buffer UpdateFavoredNodesRequest to update a list of favorednode mappings
830   * @param updateRegionInfos
831   * @return a protocol buffer UpdateFavoredNodesRequest
832   */
833  public static UpdateFavoredNodesRequest buildUpdateFavoredNodesRequest(
834      final List<Pair<HRegionInfo, List<ServerName>>> updateRegionInfos) {
835    UpdateFavoredNodesRequest.Builder ubuilder = UpdateFavoredNodesRequest.newBuilder();
836    for (Pair<HRegionInfo, List<ServerName>> pair : updateRegionInfos) {
837      RegionUpdateInfo.Builder builder = RegionUpdateInfo.newBuilder();
838      builder.setRegion(HRegionInfo.convert(pair.getFirst()));
839      for (ServerName server : pair.getSecond()) {
840        builder.addFavoredNodes(ProtobufUtil.toServerName(server));
841      }
842      ubuilder.addUpdateInfo(builder.build());
843    }
844    return ubuilder.build();
845  }
846
847  /**
848   * Create a CloseRegionRequest for a given region name
849   *
850   * @param regionName the name of the region to close
851   * @return a CloseRegionRequest
852   */
853  public static CloseRegionRequest buildCloseRegionRequest(ServerName server,
854      final byte[] regionName) {
855    return buildCloseRegionRequest(server, regionName, null);
856  }
857
858   public static CloseRegionRequest buildCloseRegionRequest(ServerName server,
859     final byte[] regionName, ServerName destinationServer) {
860     CloseRegionRequest.Builder builder = CloseRegionRequest.newBuilder();
861     RegionSpecifier region = buildRegionSpecifier(
862       RegionSpecifierType.REGION_NAME, regionName);
863     builder.setRegion(region);
864     if (destinationServer != null){
865       builder.setDestinationServer(ProtobufUtil.toServerName(destinationServer));
866     }
867     if (server != null) {
868       builder.setServerStartCode(server.getStartcode());
869     }
870     return builder.build();
871   }
872
873   /**
874    *  Create a WarmupRegionRequest for a given region name
875    *
876    *  @param regionInfo Region we are warming up
877    */
878   public static WarmupRegionRequest buildWarmupRegionRequest(final HRegionInfo regionInfo) {
879     WarmupRegionRequest.Builder builder = WarmupRegionRequest.newBuilder();
880     builder.setRegionInfo(HRegionInfo.convert(regionInfo));
881     return builder.build();
882   }
883  /**
884   * Create a CloseRegionRequest for a given encoded region name
885   *
886   * @param encodedRegionName the name of the region to close
887   * @return a CloseRegionRequest
888   */
889  public static CloseRegionRequest
890      buildCloseRegionRequest(ServerName server, final String encodedRegionName) {
891    CloseRegionRequest.Builder builder = CloseRegionRequest.newBuilder();
892    RegionSpecifier region = buildRegionSpecifier(
893      RegionSpecifierType.ENCODED_REGION_NAME,
894      Bytes.toBytes(encodedRegionName));
895    builder.setRegion(region);
896    if (server != null) {
897      builder.setServerStartCode(server.getStartcode());
898    }
899    return builder.build();
900  }
901
902  /**
903   * Create a SplitRegionRequest for a given region name
904   *
905   * @param regionName the name of the region to split
906   * @param splitPoint the split point
907   * @return a SplitRegionRequest
908   */
909  public static SplitRegionRequest buildSplitRegionRequest(
910      final byte[] regionName, final byte[] splitPoint) {
911    SplitRegionRequest.Builder builder = SplitRegionRequest.newBuilder();
912    RegionSpecifier region = buildRegionSpecifier(
913      RegionSpecifierType.REGION_NAME, regionName);
914    builder.setRegion(region);
915    if (splitPoint != null) {
916      builder.setSplitPoint(ByteStringer.wrap(splitPoint));
917    }
918    return builder.build();
919  }
920
921   /**
922    * Create a MergeRegionsRequest for the given regions
923    * @param regionA name of region a
924    * @param regionB name of region b
925    * @param forcible true if it is a compulsory merge
926    * @return a MergeRegionsRequest
927    */
928   public static MergeRegionsRequest buildMergeRegionsRequest(
929       final byte[] regionA, final byte[] regionB, final boolean forcible) {
930     MergeRegionsRequest.Builder builder = MergeRegionsRequest.newBuilder();
931     RegionSpecifier regionASpecifier = buildRegionSpecifier(
932         RegionSpecifierType.REGION_NAME, regionA);
933     RegionSpecifier regionBSpecifier = buildRegionSpecifier(
934         RegionSpecifierType.REGION_NAME, regionB);
935     builder.setRegionA(regionASpecifier);
936     builder.setRegionB(regionBSpecifier);
937     builder.setForcible(forcible);
938     // send the master's wall clock time as well, so that the RS can refer to it
939     builder.setMasterSystemTime(EnvironmentEdgeManager.currentTime());
940     return builder.build();
941   }
942
943  /**
944   * Create a  CompactRegionRequest for a given region name
945   *
946   * @param regionName the name of the region to get info
947   * @param major indicator if it is a major compaction
948   * @return a CompactRegionRequest
949   */
950  public static CompactRegionRequest buildCompactRegionRequest(
951      final byte[] regionName, final boolean major, final byte [] family) {
952    CompactRegionRequest.Builder builder = CompactRegionRequest.newBuilder();
953    RegionSpecifier region = buildRegionSpecifier(
954      RegionSpecifierType.REGION_NAME, regionName);
955    builder.setRegion(region);
956    builder.setMajor(major);
957    if (family != null) {
958      builder.setFamily(ByteStringer.wrap(family));
959    }
960    return builder.build();
961  }
962
963  /**
964   * @see {@link #buildRollWALWriterRequest()}
965   */
966  private static RollWALWriterRequest ROLL_WAL_WRITER_REQUEST =
967      RollWALWriterRequest.newBuilder().build();
968
969   /**
970   * Create a new RollWALWriterRequest
971   *
972   * @return a ReplicateWALEntryRequest
973   */
974  public static RollWALWriterRequest buildRollWALWriterRequest() {
975    return ROLL_WAL_WRITER_REQUEST;
976  }
977
978  /**
979   * @see {@link #buildGetServerInfoRequest()}
980   */
981  private static GetServerInfoRequest GET_SERVER_INFO_REQUEST =
982    GetServerInfoRequest.newBuilder().build();
983
984  /**
985   * Create a new GetServerInfoRequest
986   *
987   * @return a GetServerInfoRequest
988   */
989  public static GetServerInfoRequest buildGetServerInfoRequest() {
990    return GET_SERVER_INFO_REQUEST;
991  }
992
993  /**
994   * Create a new StopServerRequest
995   *
996   * @param reason the reason to stop the server
997   * @return a StopServerRequest
998   */
999  public static StopServerRequest buildStopServerRequest(final String reason) {
1000    StopServerRequest.Builder builder = StopServerRequest.newBuilder();
1001    builder.setReason(reason);
1002    return builder.build();
1003  }
1004
1005 //End utilities for Admin
1006
1007   /**
1008    * Convert a byte array to a protocol buffer RegionSpecifier
1009    *
1010    * @param type the region specifier type
1011    * @param value the region specifier byte array value
1012    * @return a protocol buffer RegionSpecifier
1013    */
1014   public static RegionSpecifier buildRegionSpecifier(
1015       final RegionSpecifierType type, final byte[] value) {
1016     RegionSpecifier.Builder regionBuilder = RegionSpecifier.newBuilder();
1017     regionBuilder.setValue(ByteStringer.wrap(value));
1018     regionBuilder.setType(type);
1019     return regionBuilder.build();
1020   }
1021
1022   /**
1023    * Create a protocol buffer Condition
1024    *
1025    * @param row
1026    * @param family
1027    * @param qualifier
1028    * @param comparator
1029    * @param compareType
1030    * @return a Condition
1031    * @throws IOException
1032    */
1033   private static Condition buildCondition(final byte[] row,
1034       final byte[] family, final byte [] qualifier,
1035       final ByteArrayComparable comparator,
1036       final CompareType compareType) throws IOException {
1037     Condition.Builder builder = Condition.newBuilder();
1038     builder.setRow(ByteStringer.wrap(row));
1039     builder.setFamily(ByteStringer.wrap(family));
1040     builder.setQualifier(ByteStringer.wrap(qualifier));
1041     builder.setComparator(ProtobufUtil.toComparator(comparator));
1042     builder.setCompareType(compareType);
1043     return builder.build();
1044   }
1045
1046   /**
1047    * Create a protocol buffer AddColumnRequest
1048    *
1049    * @param tableName
1050    * @param column
1051    * @return an AddColumnRequest
1052    */
1053   public static AddColumnRequest buildAddColumnRequest(
1054       final TableName tableName,
1055       final HColumnDescriptor column,
1056       final long nonceGroup,
1057       final long nonce) {
1058     AddColumnRequest.Builder builder = AddColumnRequest.newBuilder();
1059     builder.setTableName(ProtobufUtil.toProtoTableName(tableName));
1060     builder.setColumnFamilies(ProtobufUtil.convertToColumnFamilySchema(column));
1061     builder.setNonceGroup(nonceGroup);
1062     builder.setNonce(nonce);
1063     return builder.build();
1064   }
1065
1066   /**
1067    * Create a protocol buffer DeleteColumnRequest
1068    *
1069    * @param tableName
1070    * @param columnName
1071    * @return a DeleteColumnRequest
1072    */
1073   public static DeleteColumnRequest buildDeleteColumnRequest(
1074       final TableName tableName,
1075       final byte [] columnName,
1076       final long nonceGroup,
1077       final long nonce) {
1078     DeleteColumnRequest.Builder builder = DeleteColumnRequest.newBuilder();
1079     builder.setTableName(ProtobufUtil.toProtoTableName((tableName)));
1080     builder.setColumnName(ByteStringer.wrap(columnName));
1081     builder.setNonceGroup(nonceGroup);
1082     builder.setNonce(nonce);
1083     return builder.build();
1084   }
1085
1086   /**
1087    * Create a protocol buffer ModifyColumnRequest
1088    *
1089    * @param tableName
1090    * @param column
1091    * @return an ModifyColumnRequest
1092    */
1093   public static ModifyColumnRequest buildModifyColumnRequest(
1094       final TableName tableName,
1095       final HColumnDescriptor column,
1096       final long nonceGroup,
1097       final long nonce) {
1098     ModifyColumnRequest.Builder builder = ModifyColumnRequest.newBuilder();
1099     builder.setTableName(ProtobufUtil.toProtoTableName((tableName)));
1100     builder.setColumnFamilies(ProtobufUtil.convertToColumnFamilySchema(column));
1101     builder.setNonceGroup(nonceGroup);
1102     builder.setNonce(nonce);
1103     return builder.build();
1104   }
1105
1106   /**
1107    * Create a protocol buffer MoveRegionRequest
1108    *
1109    * @param encodedRegionName
1110    * @param destServerName
1111    * @return A MoveRegionRequest
1112    * @throws DeserializationException
1113    */
1114   public static MoveRegionRequest buildMoveRegionRequest(
1115       final byte [] encodedRegionName, final byte [] destServerName) throws
1116       DeserializationException {
1117     MoveRegionRequest.Builder builder = MoveRegionRequest.newBuilder();
1118     builder.setRegion(
1119       buildRegionSpecifier(RegionSpecifierType.ENCODED_REGION_NAME,encodedRegionName));
1120     if (destServerName != null) {
1121       builder.setDestServerName(
1122         ProtobufUtil.toServerName(ServerName.valueOf(Bytes.toString(destServerName))));
1123     }
1124     return builder.build();
1125   }
1126
1127   public static DispatchMergingRegionsRequest buildDispatchMergingRegionsRequest(
1128       final byte[] encodedNameOfRegionA,
1129       final byte[] encodedNameOfRegionB,
1130       final boolean forcible,
1131       final long nonceGroup,
1132       final long nonce) throws DeserializationException {
1133     DispatchMergingRegionsRequest.Builder builder = DispatchMergingRegionsRequest.newBuilder();
1134     builder.setRegionA(buildRegionSpecifier(
1135         RegionSpecifierType.ENCODED_REGION_NAME, encodedNameOfRegionA));
1136     builder.setRegionB(buildRegionSpecifier(
1137         RegionSpecifierType.ENCODED_REGION_NAME, encodedNameOfRegionB));
1138     builder.setForcible(forcible);
1139     builder.setNonceGroup(nonceGroup);
1140     builder.setNonce(nonce);
1141     return builder.build();
1142   }
1143
1144   /**
1145    * Create a protocol buffer AssignRegionRequest
1146    *
1147    * @param regionName
1148    * @return an AssignRegionRequest
1149    */
1150   public static AssignRegionRequest buildAssignRegionRequest(final byte [] regionName) {
1151     AssignRegionRequest.Builder builder = AssignRegionRequest.newBuilder();
1152     builder.setRegion(buildRegionSpecifier(RegionSpecifierType.REGION_NAME,regionName));
1153     return builder.build();
1154   }
1155
1156   /**
1157    * Creates a protocol buffer UnassignRegionRequest
1158    *
1159    * @param regionName
1160    * @param force
1161    * @return an UnassignRegionRequest
1162    */
1163   public static UnassignRegionRequest buildUnassignRegionRequest(
1164       final byte [] regionName, final boolean force) {
1165     UnassignRegionRequest.Builder builder = UnassignRegionRequest.newBuilder();
1166     builder.setRegion(buildRegionSpecifier(RegionSpecifierType.REGION_NAME,regionName));
1167     builder.setForce(force);
1168     return builder.build();
1169   }
1170
1171   /**
1172    * Creates a protocol buffer OfflineRegionRequest
1173    *
1174    * @param regionName
1175    * @return an OfflineRegionRequest
1176    */
1177   public static OfflineRegionRequest buildOfflineRegionRequest(final byte [] regionName) {
1178     OfflineRegionRequest.Builder builder = OfflineRegionRequest.newBuilder();
1179     builder.setRegion(buildRegionSpecifier(RegionSpecifierType.REGION_NAME,regionName));
1180     return builder.build();
1181   }
1182
1183   /**
1184    * Creates a protocol buffer DeleteTableRequest
1185    *
1186    * @param tableName
1187    * @return a DeleteTableRequest
1188    */
1189   public static DeleteTableRequest buildDeleteTableRequest(
1190       final TableName tableName,
1191       final long nonceGroup,
1192       final long nonce) {
1193     DeleteTableRequest.Builder builder = DeleteTableRequest.newBuilder();
1194     builder.setTableName(ProtobufUtil.toProtoTableName(tableName));
1195     builder.setNonceGroup(nonceGroup);
1196     builder.setNonce(nonce);
1197     return builder.build();
1198   }
1199
1200   /**
1201    * Creates a protocol buffer TruncateTableRequest
1202    *
1203    * @param tableName name of table to truncate
1204    * @param preserveSplits True if the splits should be preserved
1205    * @return a TruncateTableRequest
1206    */
1207   public static TruncateTableRequest buildTruncateTableRequest(
1208       final TableName tableName,
1209       final boolean preserveSplits,
1210       final long nonceGroup,
1211       final long nonce) {
1212     TruncateTableRequest.Builder builder = TruncateTableRequest.newBuilder();
1213     builder.setTableName(ProtobufUtil.toProtoTableName(tableName));
1214     builder.setPreserveSplits(preserveSplits);
1215     builder.setNonceGroup(nonceGroup);
1216     builder.setNonce(nonce);
1217     return builder.build();
1218   }
1219
1220   /**
1221    * Creates a protocol buffer EnableTableRequest
1222    *
1223    * @param tableName
1224    * @return an EnableTableRequest
1225    */
1226   public static EnableTableRequest buildEnableTableRequest(
1227       final TableName tableName,
1228       final long nonceGroup,
1229       final long nonce) {
1230     EnableTableRequest.Builder builder = EnableTableRequest.newBuilder();
1231     builder.setTableName(ProtobufUtil.toProtoTableName(tableName));
1232     builder.setNonceGroup(nonceGroup);
1233     builder.setNonce(nonce);
1234     return builder.build();
1235   }
1236
1237   /**
1238    * Creates a protocol buffer DisableTableRequest
1239    *
1240    * @param tableName
1241    * @return a DisableTableRequest
1242    */
1243   public static DisableTableRequest buildDisableTableRequest(
1244       final TableName tableName,
1245       final long nonceGroup,
1246       final long nonce) {
1247     DisableTableRequest.Builder builder = DisableTableRequest.newBuilder();
1248     builder.setTableName(ProtobufUtil.toProtoTableName((tableName)));
1249     builder.setNonceGroup(nonceGroup);
1250     builder.setNonce(nonce);
1251     return builder.build();
1252   }
1253
1254   /**
1255    * Creates a protocol buffer CreateTableRequest
1256    *
1257    * @param hTableDesc
1258    * @param splitKeys
1259    * @return a CreateTableRequest
1260    */
1261   public static CreateTableRequest buildCreateTableRequest(
1262       final HTableDescriptor hTableDesc,
1263       final byte [][] splitKeys,
1264       final long nonceGroup,
1265       final long nonce) {
1266     CreateTableRequest.Builder builder = CreateTableRequest.newBuilder();
1267     builder.setTableSchema(ProtobufUtil.convertToTableSchema(hTableDesc));
1268     if (splitKeys != null) {
1269       for (byte [] splitKey : splitKeys) {
1270         builder.addSplitKeys(ByteStringer.wrap(splitKey));
1271       }
1272     }
1273     builder.setNonceGroup(nonceGroup);
1274     builder.setNonce(nonce);
1275     return builder.build();
1276   }
1277
1278
1279   /**
1280    * Creates a protocol buffer ModifyTableRequest
1281    *
1282    * @param tableName
1283    * @param hTableDesc
1284    * @return a ModifyTableRequest
1285    */
1286   public static ModifyTableRequest buildModifyTableRequest(
1287       final TableName tableName,
1288       final HTableDescriptor hTableDesc,
1289       final long nonceGroup,
1290       final long nonce) {
1291     ModifyTableRequest.Builder builder = ModifyTableRequest.newBuilder();
1292     builder.setTableName(ProtobufUtil.toProtoTableName((tableName)));
1293     builder.setTableSchema(ProtobufUtil.convertToTableSchema(hTableDesc));
1294     builder.setNonceGroup(nonceGroup);
1295     builder.setNonce(nonce);
1296     return builder.build();
1297   }
1298
1299   /**
1300    * Creates a protocol buffer GetSchemaAlterStatusRequest
1301    *
1302    * @param tableName
1303    * @return a GetSchemaAlterStatusRequest
1304    */
1305   public static GetSchemaAlterStatusRequest buildGetSchemaAlterStatusRequest(
1306       final TableName tableName) {
1307     GetSchemaAlterStatusRequest.Builder builder = GetSchemaAlterStatusRequest.newBuilder();
1308     builder.setTableName(ProtobufUtil.toProtoTableName((tableName)));
1309     return builder.build();
1310   }
1311
1312   /**
1313    * Creates a protocol buffer GetTableDescriptorsRequest
1314    *
1315    * @param tableNames
1316    * @return a GetTableDescriptorsRequest
1317    */
1318   public static GetTableDescriptorsRequest buildGetTableDescriptorsRequest(
1319       final List<TableName> tableNames) {
1320     GetTableDescriptorsRequest.Builder builder = GetTableDescriptorsRequest.newBuilder();
1321     if (tableNames != null) {
1322       for (TableName tableName : tableNames) {
1323         builder.addTableNames(ProtobufUtil.toProtoTableName(tableName));
1324       }
1325     }
1326     return builder.build();
1327   }
1328
1329   /**
1330    * Creates a protocol buffer GetTableDescriptorsRequest
1331    *
1332    * @param pattern The compiled regular expression to match against
1333    * @param includeSysTables False to match only against userspace tables
1334    * @return a GetTableDescriptorsRequest
1335    */
1336   public static GetTableDescriptorsRequest buildGetTableDescriptorsRequest(final Pattern pattern,
1337       boolean includeSysTables) {
1338     GetTableDescriptorsRequest.Builder builder = GetTableDescriptorsRequest.newBuilder();
1339     if (pattern != null) builder.setRegex(pattern.toString());
1340     builder.setIncludeSysTables(includeSysTables);
1341     return builder.build();
1342   }
1343
1344   /**
1345    * Creates a protocol buffer GetTableNamesRequest
1346    *
1347    * @param pattern The compiled regular expression to match against
1348    * @param includeSysTables False to match only against userspace tables
1349    * @return a GetTableNamesRequest
1350    */
1351   public static GetTableNamesRequest buildGetTableNamesRequest(final Pattern pattern,
1352       boolean includeSysTables) {
1353     GetTableNamesRequest.Builder builder = GetTableNamesRequest.newBuilder();
1354     if (pattern != null) builder.setRegex(pattern.toString());
1355     builder.setIncludeSysTables(includeSysTables);
1356     return builder.build();
1357   }
1358
1359   /**
1360    * Creates a protocol buffer GetTableStateRequest
1361    *
1362    * @param tableName table to get request for
1363    * @return a GetTableStateRequest
1364    */
1365   public static GetTableStateRequest buildGetTableStateRequest(
1366           final TableName tableName) {
1367     return GetTableStateRequest.newBuilder()
1368             .setTableName(ProtobufUtil.toProtoTableName(tableName))
1369             .build();
1370   }
1371
1372   /**
1373    * Creates a protocol buffer GetTableDescriptorsRequest for a single table
1374    *
1375    * @param tableName the table name
1376    * @return a GetTableDescriptorsRequest
1377    */
1378   public static GetTableDescriptorsRequest buildGetTableDescriptorsRequest(
1379       final TableName tableName) {
1380     return GetTableDescriptorsRequest.newBuilder()
1381       .addTableNames(ProtobufUtil.toProtoTableName(tableName))
1382       .build();
1383   }
1384
1385   /**
1386    * Creates a protocol buffer IsMasterRunningRequest
1387    *
1388    * @return a IsMasterRunningRequest
1389    */
1390   public static IsMasterRunningRequest buildIsMasterRunningRequest() {
1391     return IsMasterRunningRequest.newBuilder().build();
1392   }
1393
1394   /**
1395    * Creates a protocol buffer BalanceRequest
1396    *
1397    * @return a BalanceRequest
1398    */
1399   public static BalanceRequest buildBalanceRequest(boolean force) {
1400     return BalanceRequest.newBuilder().setForce(force).build();
1401   }
1402
1403   /**
1404    * Creates a protocol buffer SetBalancerRunningRequest
1405    *
1406    * @param on
1407    * @param synchronous
1408    * @return a SetBalancerRunningRequest
1409    */
1410   public static SetBalancerRunningRequest buildSetBalancerRunningRequest(
1411       boolean on,
1412       boolean synchronous) {
1413     return SetBalancerRunningRequest.newBuilder().setOn(on).setSynchronous(synchronous).build();
1414   }
1415
1416   /**
1417    * Creates a protocol buffer IsBalancerEnabledRequest
1418    *
1419    * @return a IsBalancerEnabledRequest
1420    */
1421   public static IsBalancerEnabledRequest buildIsBalancerEnabledRequest() {
1422     return IsBalancerEnabledRequest.newBuilder().build();
1423   }
1424
1425   /**
1426    * @see {@link #buildGetClusterStatusRequest}
1427    */
1428   private static final GetClusterStatusRequest GET_CLUSTER_STATUS_REQUEST =
1429       GetClusterStatusRequest.newBuilder().build();
1430
1431   /**
1432    * Creates a protocol buffer GetClusterStatusRequest
1433    *
1434    * @return A GetClusterStatusRequest
1435    */
1436   public static GetClusterStatusRequest buildGetClusterStatusRequest() {
1437     return GET_CLUSTER_STATUS_REQUEST;
1438   }
1439
1440   /**
1441    * @see {@link #buildCatalogScanRequest}
1442    */
1443   private static final RunCatalogScanRequest CATALOG_SCAN_REQUEST =
1444     RunCatalogScanRequest.newBuilder().build();
1445
1446   /**
1447    * Creates a request for running a catalog scan
1448    * @return A {@link RunCatalogScanRequest}
1449    */
1450   public static RunCatalogScanRequest buildCatalogScanRequest() {
1451     return CATALOG_SCAN_REQUEST;
1452   }
1453
1454   /**
1455    * Creates a request for enabling/disabling the catalog janitor
1456    * @return A {@link EnableCatalogJanitorRequest}
1457    */
1458   public static EnableCatalogJanitorRequest buildEnableCatalogJanitorRequest(boolean enable) {
1459     return EnableCatalogJanitorRequest.newBuilder().setEnable(enable).build();
1460   }
1461
1462   /**
1463    * @see {@link #buildIsCatalogJanitorEnabledRequest()}
1464    */
1465   private static final IsCatalogJanitorEnabledRequest IS_CATALOG_JANITOR_ENABLED_REQUEST =
1466     IsCatalogJanitorEnabledRequest.newBuilder().build();
1467
1468   /**
1469    * Creates a request for querying the master whether the catalog janitor is enabled
1470    * @return A {@link IsCatalogJanitorEnabledRequest}
1471    */
1472   public static IsCatalogJanitorEnabledRequest buildIsCatalogJanitorEnabledRequest() {
1473     return IS_CATALOG_JANITOR_ENABLED_REQUEST;
1474   }
1475
1476   /**
1477    * Creates a request for querying the master the last flushed sequence Id for a region
1478    * @param regionName
1479    * @return A {@link GetLastFlushedSequenceIdRequest}
1480    */
1481   public static GetLastFlushedSequenceIdRequest buildGetLastFlushedSequenceIdRequest(
1482       byte[] regionName) {
1483     return GetLastFlushedSequenceIdRequest.newBuilder().setRegionName(
1484         ByteStringer.wrap(regionName)).build();
1485   }
1486
1487   /**
1488    * Create a request to grant user permissions.
1489    *
1490    * @param username the short user name who to grant permissions
1491    * @param actions the permissions to be granted
1492    * @return A {@link AccessControlProtos} GrantRequest
1493    */
1494   public static AccessControlProtos.GrantRequest buildGrantRequest(
1495       String username, AccessControlProtos.Permission.Action... actions) {
1496     AccessControlProtos.Permission.Builder ret =
1497         AccessControlProtos.Permission.newBuilder();
1498     AccessControlProtos.GlobalPermission.Builder permissionBuilder =
1499         AccessControlProtos.GlobalPermission.newBuilder();
1500     for (AccessControlProtos.Permission.Action a : actions) {
1501       permissionBuilder.addAction(a);
1502     }
1503     ret.setType(AccessControlProtos.Permission.Type.Global)
1504        .setGlobalPermission(permissionBuilder);
1505     return AccessControlProtos.GrantRequest.newBuilder()
1506       .setUserPermission(
1507           AccessControlProtos.UserPermission.newBuilder()
1508               .setUser(ByteString.copyFromUtf8(username))
1509               .setPermission(ret)
1510       ).build();
1511   }
1512
1513   /**
1514    * Create a request to grant user permissions.
1515    *
1516    * @param username the short user name who to grant permissions
1517    * @param tableName optional table name the permissions apply
1518    * @param family optional column family
1519    * @param qualifier optional qualifier
1520    * @param actions the permissions to be granted
1521    * @return A {@link AccessControlProtos} GrantRequest
1522    */
1523   public static AccessControlProtos.GrantRequest buildGrantRequest(
1524       String username, TableName tableName, byte[] family, byte[] qualifier,
1525       AccessControlProtos.Permission.Action... actions) {
1526     AccessControlProtos.Permission.Builder ret =
1527         AccessControlProtos.Permission.newBuilder();
1528     AccessControlProtos.TablePermission.Builder permissionBuilder =
1529         AccessControlProtos.TablePermission.newBuilder();
1530     for (AccessControlProtos.Permission.Action a : actions) {
1531       permissionBuilder.addAction(a);
1532     }
1533     if (tableName == null) {
1534       throw new NullPointerException("TableName cannot be null");
1535     }
1536     permissionBuilder.setTableName(ProtobufUtil.toProtoTableName(tableName));
1537
1538     if (family != null) {
1539       permissionBuilder.setFamily(ByteStringer.wrap(family));
1540     }
1541     if (qualifier != null) {
1542       permissionBuilder.setQualifier(ByteStringer.wrap(qualifier));
1543     }
1544     ret.setType(AccessControlProtos.Permission.Type.Table)
1545        .setTablePermission(permissionBuilder);
1546     return AccessControlProtos.GrantRequest.newBuilder()
1547       .setUserPermission(
1548           AccessControlProtos.UserPermission.newBuilder()
1549               .setUser(ByteString.copyFromUtf8(username))
1550               .setPermission(ret)
1551       ).build();
1552   }
1553
1554   /**
1555    * Create a request to grant user permissions.
1556    *
1557    * @param username the short user name who to grant permissions
1558    * @param namespace optional table name the permissions apply
1559    * @param actions the permissions to be granted
1560    * @return A {@link AccessControlProtos} GrantRequest
1561    */
1562   public static AccessControlProtos.GrantRequest buildGrantRequest(
1563       String username, String namespace,
1564       AccessControlProtos.Permission.Action... actions) {
1565     AccessControlProtos.Permission.Builder ret =
1566         AccessControlProtos.Permission.newBuilder();
1567     AccessControlProtos.NamespacePermission.Builder permissionBuilder =
1568         AccessControlProtos.NamespacePermission.newBuilder();
1569     for (AccessControlProtos.Permission.Action a : actions) {
1570       permissionBuilder.addAction(a);
1571     }
1572     if (namespace != null) {
1573       permissionBuilder.setNamespaceName(ByteString.copyFromUtf8(namespace));
1574     }
1575     ret.setType(AccessControlProtos.Permission.Type.Namespace)
1576        .setNamespacePermission(permissionBuilder);
1577     return AccessControlProtos.GrantRequest.newBuilder()
1578       .setUserPermission(
1579           AccessControlProtos.UserPermission.newBuilder()
1580               .setUser(ByteString.copyFromUtf8(username))
1581               .setPermission(ret)
1582       ).build();
1583   }
1584
1585   /**
1586    * Create a request to revoke user permissions.
1587    *
1588    * @param username the short user name whose permissions to be revoked
1589    * @param actions the permissions to be revoked
1590    * @return A {@link AccessControlProtos} RevokeRequest
1591    */
1592   public static AccessControlProtos.RevokeRequest buildRevokeRequest(
1593       String username, AccessControlProtos.Permission.Action... actions) {
1594     AccessControlProtos.Permission.Builder ret =
1595         AccessControlProtos.Permission.newBuilder();
1596     AccessControlProtos.GlobalPermission.Builder permissionBuilder =
1597         AccessControlProtos.GlobalPermission.newBuilder();
1598     for (AccessControlProtos.Permission.Action a : actions) {
1599       permissionBuilder.addAction(a);
1600     }
1601     ret.setType(AccessControlProtos.Permission.Type.Global)
1602        .setGlobalPermission(permissionBuilder);
1603     return AccessControlProtos.RevokeRequest.newBuilder()
1604       .setUserPermission(
1605           AccessControlProtos.UserPermission.newBuilder()
1606               .setUser(ByteString.copyFromUtf8(username))
1607               .setPermission(ret)
1608       ).build();
1609   }
1610
1611   /**
1612    * Create a request to revoke user permissions.
1613    *
1614    * @param username the short user name whose permissions to be revoked
1615    * @param tableName optional table name the permissions apply
1616    * @param family optional column family
1617    * @param qualifier optional qualifier
1618    * @param actions the permissions to be revoked
1619    * @return A {@link AccessControlProtos} RevokeRequest
1620    */
1621   public static AccessControlProtos.RevokeRequest buildRevokeRequest(
1622       String username, TableName tableName, byte[] family, byte[] qualifier,
1623       AccessControlProtos.Permission.Action... actions) {
1624     AccessControlProtos.Permission.Builder ret =
1625         AccessControlProtos.Permission.newBuilder();
1626     AccessControlProtos.TablePermission.Builder permissionBuilder =
1627         AccessControlProtos.TablePermission.newBuilder();
1628     for (AccessControlProtos.Permission.Action a : actions) {
1629       permissionBuilder.addAction(a);
1630     }
1631     if (tableName != null) {
1632       permissionBuilder.setTableName(ProtobufUtil.toProtoTableName(tableName));
1633     }
1634     if (family != null) {
1635       permissionBuilder.setFamily(ByteStringer.wrap(family));
1636     }
1637     if (qualifier != null) {
1638       permissionBuilder.setQualifier(ByteStringer.wrap(qualifier));
1639     }
1640     ret.setType(AccessControlProtos.Permission.Type.Table)
1641        .setTablePermission(permissionBuilder);
1642     return AccessControlProtos.RevokeRequest.newBuilder()
1643       .setUserPermission(
1644           AccessControlProtos.UserPermission.newBuilder()
1645               .setUser(ByteString.copyFromUtf8(username))
1646               .setPermission(ret)
1647       ).build();
1648   }
1649
1650   /**
1651    * Create a request to revoke user permissions.
1652    *
1653    * @param username the short user name whose permissions to be revoked
1654    * @param namespace optional table name the permissions apply
1655    * @param actions the permissions to be revoked
1656    * @return A {@link AccessControlProtos} RevokeRequest
1657    */
1658   public static AccessControlProtos.RevokeRequest buildRevokeRequest(
1659       String username, String namespace,
1660       AccessControlProtos.Permission.Action... actions) {
1661     AccessControlProtos.Permission.Builder ret =
1662         AccessControlProtos.Permission.newBuilder();
1663     AccessControlProtos.NamespacePermission.Builder permissionBuilder =
1664         AccessControlProtos.NamespacePermission.newBuilder();
1665     for (AccessControlProtos.Permission.Action a : actions) {
1666       permissionBuilder.addAction(a);
1667     }
1668     if (namespace != null) {
1669       permissionBuilder.setNamespaceName(ByteString.copyFromUtf8(namespace));
1670     }
1671     ret.setType(AccessControlProtos.Permission.Type.Namespace)
1672        .setNamespacePermission(permissionBuilder);
1673     return AccessControlProtos.RevokeRequest.newBuilder()
1674       .setUserPermission(
1675           AccessControlProtos.UserPermission.newBuilder()
1676               .setUser(ByteString.copyFromUtf8(username))
1677               .setPermission(ret)
1678       ).build();
1679   }
1680
1681   /**
1682    * Create a RegionOpenInfo based on given region info and version of offline node
1683    */
1684   private static RegionOpenInfo buildRegionOpenInfo(
1685       final HRegionInfo region,
1686       final List<ServerName> favoredNodes, Boolean openForReplay) {
1687     RegionOpenInfo.Builder builder = RegionOpenInfo.newBuilder();
1688     builder.setRegion(HRegionInfo.convert(region));
1689     if (favoredNodes != null) {
1690       for (ServerName server : favoredNodes) {
1691         builder.addFavoredNodes(ProtobufUtil.toServerName(server));
1692       }
1693     }
1694     if(openForReplay != null) {
1695       builder.setOpenForDistributedLogReplay(openForReplay);
1696     }
1697     return builder.build();
1698   }
1699
1700   /**
1701    * Creates a protocol buffer NormalizeRequest
1702    *
1703    * @return a NormalizeRequest
1704    */
1705   public static NormalizeRequest buildNormalizeRequest() {
1706     return NormalizeRequest.newBuilder().build();
1707   }
1708
1709   /**
1710    * Creates a protocol buffer IsNormalizerEnabledRequest
1711    *
1712    * @return a IsNormalizerEnabledRequest
1713    */
1714   public static IsNormalizerEnabledRequest buildIsNormalizerEnabledRequest() {
1715     return IsNormalizerEnabledRequest.newBuilder().build();
1716   }
1717
1718   /**
1719    * Creates a protocol buffer SetNormalizerRunningRequest
1720    *
1721    * @param on
1722    * @return a SetNormalizerRunningRequest
1723    */
1724   public static SetNormalizerRunningRequest buildSetNormalizerRunningRequest(boolean on) {
1725     return SetNormalizerRunningRequest.newBuilder().setOn(on).build();
1726   }
1727
1728   /**
1729    * Creates a protocol buffer IsSplitOrMergeEnabledRequest
1730    *
1731    * @param switchType see {@link org.apache.hadoop.hbase.client.MasterSwitchType}
1732    * @return a IsSplitOrMergeEnabledRequest
1733    */
1734   public static IsSplitOrMergeEnabledRequest buildIsSplitOrMergeEnabledRequest(
1735     MasterSwitchType switchType) {
1736     IsSplitOrMergeEnabledRequest.Builder builder = IsSplitOrMergeEnabledRequest.newBuilder();
1737     builder.setSwitchType(convert(switchType));
1738     return builder.build();
1739   }
1740
1741   /**
1742    * Creates a protocol buffer SetSplitOrMergeEnabledRequest
1743    *
1744    * @param enabled switch is enabled or not
1745    * @param synchronous set switch sync?
1746    * @param switchTypes see {@link org.apache.hadoop.hbase.client.MasterSwitchType}, it is
1747    *                    a list.
1748    * @return a SetSplitOrMergeEnabledRequest
1749    */
1750   public static SetSplitOrMergeEnabledRequest buildSetSplitOrMergeEnabledRequest(boolean enabled,
1751     boolean synchronous, MasterSwitchType... switchTypes) {
1752     SetSplitOrMergeEnabledRequest.Builder builder = SetSplitOrMergeEnabledRequest.newBuilder();
1753     builder.setEnabled(enabled);
1754     builder.setSynchronous(synchronous);
1755     for (MasterSwitchType switchType : switchTypes) {
1756       builder.addSwitchTypes(convert(switchType));
1757     }
1758     return builder.build();
1759   }
1760
1761   private static MasterProtos.MasterSwitchType convert(MasterSwitchType switchType) {
1762     switch (switchType) {
1763       case SPLIT:
1764         return MasterProtos.MasterSwitchType.SPLIT;
1765       case MERGE:
1766         return MasterProtos.MasterSwitchType.MERGE;
1767       default:
1768         break;
1769     }
1770     throw new UnsupportedOperationException("Unsupport switch type:" + switchType);
1771   }
1772 }