-
Notifications
You must be signed in to change notification settings - Fork 81
/
AppProfile.java
412 lines (362 loc) · 13.8 KB
/
AppProfile.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigtable.admin.v2.models;
import com.google.api.core.InternalApi;
import com.google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny;
import com.google.bigtable.admin.v2.AppProfile.Priority;
import com.google.bigtable.admin.v2.AppProfile.StandardIsolation;
import com.google.bigtable.admin.v2.AppProfileName;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableSet;
import java.util.Set;
import javax.annotation.Nonnull;
/**
* An application profile, or app profile, stores settings that tell your Cloud Bigtable instance
* how to handle incoming requests from an application. When one of your applications connects to a
* Cloud Bigtable instance, it can specify an app profile, and Cloud Bigtable uses that app profile
* for any requests that the application sends over that connection.
*
* <p>App profiles affect how your applications communicate with an instance that uses replication.
* As a result, app profiles are especially useful for instances that have 2 clusters. Even if your
* instance has only 1 cluster, you can use a unique app profile for each application that you run,
* or for different components within a single application.
*
* @see <a href="https://cloud.google.com/bigtable/docs/app-profiles">For more details</a>.
*/
public final class AppProfile {
private final com.google.bigtable.admin.v2.AppProfile proto;
/**
* Wraps a protobuf response.
*
* <p>This method is considered an internal implementation detail and not meant to be used by
* applications.
*/
@InternalApi
public static AppProfile fromProto(@Nonnull com.google.bigtable.admin.v2.AppProfile proto) {
return new AppProfile(proto);
}
private AppProfile(@Nonnull com.google.bigtable.admin.v2.AppProfile proto) {
Preconditions.checkNotNull(proto);
Preconditions.checkArgument(
proto.hasSingleClusterRouting() || proto.hasMultiClusterRoutingUseAny(),
"AppProfile must have a routing policy");
Preconditions.checkArgument(!proto.getName().isEmpty(), "AppProfile must have a name");
this.proto = proto;
}
/** Gets the routing policy for all read/write requests which use this app profile. */
@SuppressWarnings("WeakerAccess")
public RoutingPolicy getPolicy() {
if (proto.hasMultiClusterRoutingUseAny()) {
return MultiClusterRoutingPolicy.of(
ImmutableSet.copyOf(proto.getMultiClusterRoutingUseAny().getClusterIdsList()));
} else if (proto.hasSingleClusterRouting()) {
return new SingleClusterRoutingPolicy(proto.getSingleClusterRouting());
} else {
// Should never happen because the constructor verifies that one must exist.
throw new VerifyException();
}
}
public IsolationPolicy getIsolationPolicy() {
if (proto.hasStandardIsolation()) {
return new StandardIsolationPolicy(proto.getStandardIsolation());
} else {
// Should never happen because the constructor verifies that one must exist.
throw new IllegalStateException();
}
}
/** Gets the id of this AppProfile. */
@SuppressWarnings("WeakerAccess")
public String getId() {
AppProfileName fullName =
Verify.verifyNotNull(AppProfileName.parse(proto.getName()), "Name can never be null");
//noinspection ConstantConditions
return fullName.getAppProfile();
}
/** Gets the id of the instance that owns this AppProfile. */
@SuppressWarnings("WeakerAccess")
public String getInstanceId() {
AppProfileName fullName =
Verify.verifyNotNull(AppProfileName.parse(proto.getName()), "Name can never be null");
//noinspection ConstantConditions
return fullName.getInstance();
}
/** Gets long form description of the use case for this AppProfile. */
@SuppressWarnings("WeakerAccess")
public String getDescription() {
return proto.getDescription();
}
/**
* Creates the request protobuf. This method is considered an internal implementation detail and
* not meant to be used by applications.
*/
com.google.bigtable.admin.v2.AppProfile toProto() {
return proto;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AppProfile that = (AppProfile) o;
return Objects.equal(proto, that.proto);
}
@Override
public int hashCode() {
return Objects.hashCode(proto);
}
/**
* Represents the routing for read/write requests. Please check the implementations of this
* interface for more details.
*/
@SuppressWarnings("WeakerAccess")
public interface RoutingPolicy {}
/** A {@link RoutingPolicy} that routes all requests to a specific cluster. */
@SuppressWarnings("WeakerAccess")
public static class SingleClusterRoutingPolicy implements RoutingPolicy {
private final com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting proto;
/**
* Wraps a protobuf response.
*
* <p>This method is considered an internal implementation detail and not meant to be used by
* applications.
*/
@InternalApi
public static SingleClusterRoutingPolicy fromProto(
com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting proto) {
return new SingleClusterRoutingPolicy(proto);
}
/**
* Builds a new instance of the routing policy that will send all requests to the specified
* cluster.
*
* <p>Please note that atomic row transactions will be disabled.
*/
public static SingleClusterRoutingPolicy of(String clusterId) {
return of(clusterId, false);
}
/**
* Builds a new instance of the routing policy that will send all requests to the specified
* cluster. This variant enables the ability to re-enable single row transactions at the cost of
* consistency.
*
* <p>Please see the <a
* href="https://cloud.google.com/bigtable/docs/app-profiles#single-row-transactions">online
* documentation</a> for more details.
*/
public static SingleClusterRoutingPolicy of(String clusterId, boolean allowTransactionWrites) {
return fromProto(
com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting.newBuilder()
.setClusterId(clusterId)
.setAllowTransactionalWrites(allowTransactionWrites)
.build());
}
private SingleClusterRoutingPolicy(
com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting proto) {
this.proto = proto;
}
/** Gets the target cluster of this policy. */
@SuppressWarnings("WeakerAccess")
public String getClusterId() {
return proto.getClusterId();
}
/** Checks if transactional writes are enabled. */
@SuppressWarnings("WeakerAccess")
public boolean getAllowTransactionalWrites() {
return proto.getAllowTransactionalWrites();
}
/**
* Wraps a protobuf response.
*
* <p>This method is considered an internal implementation detail and not meant to be used by
* applications.
*/
@InternalApi
com.google.bigtable.admin.v2.AppProfile.SingleClusterRouting toProto() {
return proto;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SingleClusterRoutingPolicy that = (SingleClusterRoutingPolicy) o;
return Objects.equal(proto, that.proto);
}
@Override
public int hashCode() {
return Objects.hashCode(proto);
}
}
/**
* A {@link RoutingPolicy} that tells Cloud Bigtable that it can route each request to any
* available cluster.
*/
public static class MultiClusterRoutingPolicy implements RoutingPolicy {
private final MultiClusterRoutingUseAny proto;
/** Creates a new instance of {@link MultiClusterRoutingPolicy}. */
public static MultiClusterRoutingPolicy of() {
return new MultiClusterRoutingPolicy(MultiClusterRoutingUseAny.getDefaultInstance());
}
/**
* Creates a new instance of {@link MultiClusterRoutingPolicy} with specified cluster ids to
* route to.
*/
public static MultiClusterRoutingPolicy of(String... clusterIds) {
return of(ImmutableSet.copyOf(clusterIds));
}
/**
* Creates a new instance of {@link MultiClusterRoutingPolicy} with specified cluster ids to
* route to.
*/
public static MultiClusterRoutingPolicy of(Set<String> clusterIds) {
return new MultiClusterRoutingPolicy(
MultiClusterRoutingUseAny.newBuilder().addAllClusterIds(clusterIds).build());
}
/*
* Returns the set of clusters to route to. The order is ignored; clusters will be
* tried in order of distance. If empty, all clusters are eligible.
*/
public Set<String> getClusterIds() {
return ImmutableSet.copyOf(proto.getClusterIdsList());
}
private MultiClusterRoutingPolicy(
com.google.bigtable.admin.v2.AppProfile.MultiClusterRoutingUseAny proto) {
this.proto = proto;
}
/**
* Creates the request protobuf. This method is considered an internal implementation detail and
* not meant to be used by applications.
*/
@InternalApi
MultiClusterRoutingUseAny toProto() {
return proto;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
MultiClusterRoutingPolicy that = (MultiClusterRoutingPolicy) o;
return Objects.equal(proto, that.proto);
}
@Override
public int hashCode() {
return Objects.hashCode(proto);
}
}
/** Represents the options for isolating this app profile's traffic from other use cases. */
@SuppressWarnings("WeakerAccess")
public interface IsolationPolicy {}
/**
* The possible priorities for an app profile. Note that higher priority writes can sometimes
* queue behind lower priority writes to the same tablet, as writes must be strictly sequenced in
* the durability log.
*/
public static enum Priority {
LOW(com.google.bigtable.admin.v2.AppProfile.Priority.PRIORITY_LOW),
MEDIUM(com.google.bigtable.admin.v2.AppProfile.Priority.PRIORITY_MEDIUM),
HIGH(com.google.bigtable.admin.v2.AppProfile.Priority.PRIORITY_HIGH);
private final com.google.bigtable.admin.v2.AppProfile.Priority proto;
/**
* Wraps the protobuf. This method is considered an internal implementation detail and not meant
* to be used by applications.
*/
@InternalApi
public static Priority fromProto(com.google.bigtable.admin.v2.AppProfile.Priority proto) {
Preconditions.checkNotNull(proto);
for (Priority priority : values()) {
if (priority.proto.equals(proto)) {
return priority;
}
}
throw new IllegalArgumentException("Unknown priority: " + proto);
}
Priority(com.google.bigtable.admin.v2.AppProfile.Priority proto) {
this.proto = proto;
}
/**
* Creates the request protobuf. This method is considered an internal implementation detail and
* not meant to be used by applications.
*/
@InternalApi
public com.google.bigtable.admin.v2.AppProfile.Priority toProto() {
return proto;
}
}
/**
* A standard {@link IsolationPolicy} for isolating this app profile's traffic from other use
* cases. This accomplished by assigning different priorities to app profiles. A request that uses
* an app profile with a StandardIsolationPolicy with a HIGH priority will likely run before a
* request with a LOW priority.
*/
public static class StandardIsolationPolicy implements IsolationPolicy {
private final StandardIsolation proto;
/** Creates a new instance of {@link StandardIsolationPolicy}. */
public static StandardIsolationPolicy of() {
return new StandardIsolationPolicy(StandardIsolation.getDefaultInstance());
}
/** Creates a new instance of {@link StandardIsolationPolicy} with the specified priority. */
public static StandardIsolationPolicy of(Priority priority) {
return new StandardIsolationPolicy(
StandardIsolation.newBuilder().setPriority(priority.toProto()).build());
}
/*
* Returns the priority for this app profile.
*/
public Priority getPriority() {
return Priority.fromProto(proto.getPriority());
}
private StandardIsolationPolicy(
com.google.bigtable.admin.v2.AppProfile.StandardIsolation proto) {
this.proto = proto;
}
/**
* Creates the request protobuf. This method is considered an internal implementation detail and
* not meant to be used by applications.
*/
@InternalApi
com.google.bigtable.admin.v2.AppProfile.StandardIsolation toProto() {
return proto;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
StandardIsolationPolicy that = (StandardIsolationPolicy) o;
return Objects.equal(proto, that.proto);
}
@Override
public int hashCode() {
return Objects.hashCode(proto);
}
}
}