Search in sources :

Example 1 with RemoveServerCacheResponse

use of org.apache.phoenix.coprocessor.generated.ServerCachingProtos.RemoveServerCacheResponse in project phoenix by apache.

the class ServerCachingEndpointImpl method removeServerCache.

@Override
public void removeServerCache(RpcController controller, RemoveServerCacheRequest request, RpcCallback<RemoveServerCacheResponse> done) {
    ImmutableBytesPtr tenantId = null;
    if (request.hasTenantId()) {
        tenantId = new ImmutableBytesPtr(request.getTenantId().toByteArray());
    }
    TenantCache tenantCache = GlobalCache.getTenantCache(this.env, tenantId);
    tenantCache.removeServerCache(new ImmutableBytesPtr(request.getCacheId().toByteArray()));
    RemoveServerCacheResponse.Builder responseBuilder = RemoveServerCacheResponse.newBuilder();
    responseBuilder.setReturn(true);
    RemoveServerCacheResponse result = responseBuilder.build();
    done.run(result);
}
Also used : TenantCache(org.apache.phoenix.cache.TenantCache) RemoveServerCacheResponse(org.apache.phoenix.coprocessor.generated.ServerCachingProtos.RemoveServerCacheResponse) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)

Example 2 with RemoveServerCacheResponse

use of org.apache.phoenix.coprocessor.generated.ServerCachingProtos.RemoveServerCacheResponse in project phoenix by apache.

the class ServerCacheClient method removeServerCache.

/**
     * Remove the cached table from all region servers
     * @param cacheId unique identifier for the hash join (returned from {@link #addHashCache(HTable, Scan, Set)})
     * @param servers list of servers upon which table was cached (filled in by {@link #addHashCache(HTable, Scan, Set)})
     * @throws SQLException
     * @throws IllegalStateException if hashed table cannot be removed on any region server on which it was added
     */
private void removeServerCache(final byte[] cacheId, Set<HRegionLocation> servers) throws SQLException {
    ConnectionQueryServices services = connection.getQueryServices();
    Throwable lastThrowable = null;
    TableRef cacheUsingTableRef = cacheUsingTableRefMap.get(Bytes.mapKey(cacheId));
    final PTable cacheUsingTable = cacheUsingTableRef.getTable();
    byte[] tableName = cacheUsingTableRef.getTable().getPhysicalName().getBytes();
    HTableInterface iterateOverTable = services.getTable(tableName);
    try {
        List<HRegionLocation> locations = services.getAllTableRegions(tableName);
        Set<HRegionLocation> remainingOnServers = new HashSet<HRegionLocation>(servers);
        /**
    		 * Allow for the possibility that the region we based where to send our cache has split and been
    		 * relocated to another region server *after* we sent it, but before we removed it. To accommodate
    		 * this, we iterate through the current metadata boundaries and remove the cache once for each
    		 * server that we originally sent to.
    		 */
        if (LOG.isDebugEnabled()) {
            LOG.debug(addCustomAnnotations("Removing Cache " + cacheId + " from servers.", connection));
        }
        for (HRegionLocation entry : locations) {
            if (remainingOnServers.contains(entry)) {
                // Call once per server
                try {
                    byte[] key = getKeyInRegion(entry.getRegionInfo().getStartKey());
                    iterateOverTable.coprocessorService(ServerCachingService.class, key, key, new Batch.Call<ServerCachingService, RemoveServerCacheResponse>() {

                        @Override
                        public RemoveServerCacheResponse call(ServerCachingService instance) throws IOException {
                            ServerRpcController controller = new ServerRpcController();
                            BlockingRpcCallback<RemoveServerCacheResponse> rpcCallback = new BlockingRpcCallback<RemoveServerCacheResponse>();
                            RemoveServerCacheRequest.Builder builder = RemoveServerCacheRequest.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));
                            instance.removeServerCache(controller, builder.build(), rpcCallback);
                            if (controller.getFailedOn() != null) {
                                throw controller.getFailedOn();
                            }
                            return rpcCallback.get();
                        }
                    });
                    remainingOnServers.remove(entry);
                } catch (Throwable t) {
                    lastThrowable = t;
                    LOG.error(addCustomAnnotations("Error trying to remove hash cache for " + entry, connection), t);
                }
            }
        }
        if (!remainingOnServers.isEmpty()) {
            LOG.warn(addCustomAnnotations("Unable to remove hash cache for " + remainingOnServers, connection), lastThrowable);
        }
    } finally {
        Closeables.closeQuietly(iterateOverTable);
    }
}
Also used : RemoveServerCacheResponse(org.apache.phoenix.coprocessor.generated.ServerCachingProtos.RemoveServerCacheResponse) SQLException(java.sql.SQLException) ServerCachingService(org.apache.phoenix.coprocessor.generated.ServerCachingProtos.ServerCachingService) IOException(java.io.IOException) HTableInterface(org.apache.hadoop.hbase.client.HTableInterface) ServerRpcController(org.apache.hadoop.hbase.ipc.ServerRpcController) PTable(org.apache.phoenix.schema.PTable) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) Batch(org.apache.hadoop.hbase.client.coprocessor.Batch) BlockingRpcCallback(org.apache.hadoop.hbase.ipc.BlockingRpcCallback) ConnectionQueryServices(org.apache.phoenix.query.ConnectionQueryServices) TableRef(org.apache.phoenix.schema.TableRef) HashSet(java.util.HashSet)

Aggregations

RemoveServerCacheResponse (org.apache.phoenix.coprocessor.generated.ServerCachingProtos.RemoveServerCacheResponse)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)1 HTableInterface (org.apache.hadoop.hbase.client.HTableInterface)1 Batch (org.apache.hadoop.hbase.client.coprocessor.Batch)1 BlockingRpcCallback (org.apache.hadoop.hbase.ipc.BlockingRpcCallback)1 ServerRpcController (org.apache.hadoop.hbase.ipc.ServerRpcController)1 TenantCache (org.apache.phoenix.cache.TenantCache)1 ServerCachingService (org.apache.phoenix.coprocessor.generated.ServerCachingProtos.ServerCachingService)1 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)1 ConnectionQueryServices (org.apache.phoenix.query.ConnectionQueryServices)1 PTable (org.apache.phoenix.schema.PTable)1 TableRef (org.apache.phoenix.schema.TableRef)1