Github user JamesRTaylor commented on a diff in the pull request:
https://github.com/apache/phoenix/pull/268#discussion_r129185421
--- Diff:
phoenix-core/src/main/java/org/apache/phoenix/cache/ServerCacheClient.java ---
@@ -394,4 +420,82 @@ public static String idToString(byte[] uuid) {
}
return regionStartKey;
}
+
+ public boolean addServerCache(byte[] startkeyOfRegion, ServerCache
cache, HashCacheFactory cacheFactory,
+ byte[] txState, PTable pTable) throws Exception {
+ HTableInterface table = null;
+ byte[] cacheId = cache.getId();
+ try {
+ ConnectionQueryServices services =
connection.getQueryServices();
+ byte[] tableName = pTable.getPhysicalName().getBytes();
+ table = services.getTable(tableName);
+ boolean success = addServerCache(table, startkeyOfRegion,
pTable, cacheId, cache.getCachePtr(), cacheFactory, txState);
+ //track keys so that we can remove the hash table cache from
the new regionservers where the cache was re-sent
+ cache.addKey(startkeyOfRegion);
+ return success;
+ } finally {
+ Closeables.closeQuietly(table);
+ }
+ }
+
+ public boolean addServerCache(HTableInterface htable, byte[] key,
final PTable cacheUsingTable, final byte[] cacheId,
+ final ImmutableBytesWritable cachePtr, final
ServerCacheFactory cacheFactory, final byte[] txState)
+ throws Exception {
+ byte[] keyInRegion = getKeyInRegion(key);
+ final Map<byte[], AddServerCacheResponse> results;
+ try {
+ results =
htable.coprocessorService(ServerCachingService.class, keyInRegion, keyInRegion,
+ new Batch.Call<ServerCachingService,
AddServerCacheResponse>() {
+ @Override
+ public AddServerCacheResponse
call(ServerCachingService instance) throws IOException {
+ ServerRpcController controller = new
ServerRpcController();
+ BlockingRpcCallback<AddServerCacheResponse>
rpcCallback = new BlockingRpcCallback<AddServerCacheResponse>();
+ AddServerCacheRequest.Builder builder =
AddServerCacheRequest.newBuilder();
+ final byte[] tenantIdBytes;
+ if (cacheUsingTable.isMultiTenant()) {
+ try {
+ tenantIdBytes =
connection.getTenantId() == null ? null
+ :
ScanUtil.getTenantIdBytes(cacheUsingTable.getRowKeySchema(),
+
cacheUsingTable.getBucketNum() != null, connection.getTenantId(),
+
cacheUsingTable.getViewIndexId() != null);
+ } catch (SQLException e) {
+ throw new IOException(e);
+ }
+ } else {
+ tenantIdBytes = connection.getTenantId()
== null ? null
+ :
connection.getTenantId().getBytes();
+ }
+ if (tenantIdBytes != null) {
+
builder.setTenantId(ByteStringer.wrap(tenantIdBytes));
+ }
+ builder.setCacheId(ByteStringer.wrap(cacheId));
+
builder.setCachePtr(org.apache.phoenix.protobuf.ProtobufUtil.toProto(cachePtr));
+ builder.setHasProtoBufIndexMaintainer(true);
+
ServerCacheFactoryProtos.ServerCacheFactory.Builder svrCacheFactoryBuider =
ServerCacheFactoryProtos.ServerCacheFactory
+ .newBuilder();
+
svrCacheFactoryBuider.setClassName(cacheFactory.getClass().getName());
+
builder.setCacheFactory(svrCacheFactoryBuider.build());
+ builder.setTxState(ByteStringer.wrap(txState));
+ instance.addServerCache(controller,
builder.build(), rpcCallback);
+ if (controller.getFailedOn() != null) { throw
controller.getFailedOn(); }
+ return rpcCallback.get();
+ }
+ });
+ } catch (Throwable t) {
+ throw new Exception(t);
+ }
+ if (results != null && results.size() == 1) { return
results.values().iterator().next().getReturn(); }
+ return false;
+
+ }
+
+ public static ServerCache getCacheForId(List<ServerCache> caches, Long
cacheId) {
--- End diff --
How about using a Map<ImmutableBytesPtr,ServerCache> instead of a List and
switching id to ImmutableBytesPtr? Then you can get rid of this static method.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---