Search in sources :

Example 1 with TenantCache

use of org.apache.phoenix.cache.TenantCache in project phoenix by apache.

the class ServerCachingEndpointImpl method addServerCache.

@Override
public void addServerCache(RpcController controller, AddServerCacheRequest request, RpcCallback<AddServerCacheResponse> done) {
    ImmutableBytesPtr tenantId = null;
    if (request.hasTenantId()) {
        tenantId = new ImmutableBytesPtr(request.getTenantId().toByteArray());
    }
    TenantCache tenantCache = GlobalCache.getTenantCache(this.env, tenantId);
    ImmutableBytesWritable cachePtr = org.apache.phoenix.protobuf.ProtobufUtil.toImmutableBytesWritable(request.getCachePtr());
    byte[] txState = request.hasTxState() ? request.getTxState().toByteArray() : ByteUtil.EMPTY_BYTE_ARRAY;
    try {
        @SuppressWarnings("unchecked") Class<ServerCacheFactory> serverCacheFactoryClass = (Class<ServerCacheFactory>) Class.forName(request.getCacheFactory().getClassName());
        ServerCacheFactory cacheFactory = serverCacheFactoryClass.newInstance();
        tenantCache.addServerCache(new ImmutableBytesPtr(request.getCacheId().toByteArray()), cachePtr, txState, cacheFactory, request.hasHasProtoBufIndexMaintainer() && request.getHasProtoBufIndexMaintainer());
    } catch (Throwable e) {
        ProtobufUtil.setControllerException(controller, new IOException(e));
    }
    AddServerCacheResponse.Builder responseBuilder = AddServerCacheResponse.newBuilder();
    responseBuilder.setReturn(true);
    AddServerCacheResponse result = responseBuilder.build();
    done.run(result);
}
Also used : TenantCache(org.apache.phoenix.cache.TenantCache) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) IOException(java.io.IOException) ServerCacheFactory(org.apache.phoenix.coprocessor.ServerCachingProtocol.ServerCacheFactory) AddServerCacheResponse(org.apache.phoenix.coprocessor.generated.ServerCachingProtos.AddServerCacheResponse)

Example 2 with TenantCache

use of org.apache.phoenix.cache.TenantCache 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 3 with TenantCache

use of org.apache.phoenix.cache.TenantCache in project phoenix by apache.

the class PhoenixIndexMetaData method getIndexMetaData.

private static IndexMetaDataCache getIndexMetaData(RegionCoprocessorEnvironment env, Map<String, byte[]> attributes) throws IOException {
    if (attributes == null) {
        return IndexMetaDataCache.EMPTY_INDEX_META_DATA_CACHE;
    }
    byte[] uuid = attributes.get(PhoenixIndexCodec.INDEX_UUID);
    if (uuid == null) {
        return IndexMetaDataCache.EMPTY_INDEX_META_DATA_CACHE;
    }
    boolean useProto = false;
    byte[] md = attributes.get(PhoenixIndexCodec.INDEX_PROTO_MD);
    useProto = md != null;
    if (md == null) {
        md = attributes.get(PhoenixIndexCodec.INDEX_MD);
    }
    byte[] txState = attributes.get(BaseScannerRegionObserver.TX_STATE);
    if (md != null) {
        final List<IndexMaintainer> indexMaintainers = IndexMaintainer.deserialize(md, useProto);
        final Transaction txn = MutationState.decodeTransaction(txState);
        return new IndexMetaDataCache() {

            @Override
            public void close() throws IOException {
            }

            @Override
            public List<IndexMaintainer> getIndexMaintainers() {
                return indexMaintainers;
            }

            @Override
            public Transaction getTransaction() {
                return txn;
            }
        };
    } else {
        byte[] tenantIdBytes = attributes.get(PhoenixRuntime.TENANT_ID_ATTRIB);
        ImmutableBytesPtr tenantId = tenantIdBytes == null ? null : new ImmutableBytesPtr(tenantIdBytes);
        TenantCache cache = GlobalCache.getTenantCache(env, tenantId);
        IndexMetaDataCache indexCache = (IndexMetaDataCache) cache.getServerCache(new ImmutableBytesPtr(uuid));
        if (indexCache == null) {
            String msg = "key=" + ServerCacheClient.idToString(uuid) + " region=" + env.getRegion() + "host=" + env.getRegionServerServices().getServerName();
            SQLException e = new SQLExceptionInfo.Builder(SQLExceptionCode.INDEX_METADATA_NOT_FOUND).setMessage(msg).build().buildException();
            // will not return
            ServerUtil.throwIOException("Index update failed", e);
        }
        return indexCache;
    }
}
Also used : IndexMetaDataCache(org.apache.phoenix.cache.IndexMetaDataCache) TenantCache(org.apache.phoenix.cache.TenantCache) Transaction(org.apache.tephra.Transaction) SQLException(java.sql.SQLException) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Example 4 with TenantCache

use of org.apache.phoenix.cache.TenantCache in project phoenix by apache.

the class NonAggregateRegionScannerFactory method getTopNScanner.

/**
   *  Return region scanner that does TopN.
   *  We only need to call startRegionOperation and closeRegionOperation when
   *  getting the first Tuple (which forces running through the entire region)
   *  since after this everything is held in memory
   */
private RegionScanner getTopNScanner(RegionCoprocessorEnvironment env, final RegionScanner s, final OrderedResultIterator iterator, ImmutableBytesPtr tenantId) throws Throwable {
    final Tuple firstTuple;
    TenantCache tenantCache = GlobalCache.getTenantCache(env, tenantId);
    long estSize = iterator.getEstimatedByteSize();
    final MemoryManager.MemoryChunk chunk = tenantCache.getMemoryManager().allocate(estSize);
    final Region region = getRegion();
    region.startRegionOperation();
    try {
        // Once we return from the first call to next, we've run through and cached
        // the topN rows, so we no longer need to start/stop a region operation.
        firstTuple = iterator.next();
        // Now that the topN are cached, we can resize based on the real size
        long actualSize = iterator.getByteSize();
        chunk.resize(actualSize);
    } catch (Throwable t) {
        ServerUtil.throwIOException(region.getRegionInfo().getRegionNameAsString(), t);
        return null;
    } finally {
        region.closeRegionOperation();
    }
    return new BaseRegionScanner(s) {

        private Tuple tuple = firstTuple;

        @Override
        public boolean isFilterDone() {
            return tuple == null;
        }

        @Override
        public boolean next(List<Cell> results) throws IOException {
            try {
                if (isFilterDone()) {
                    return false;
                }
                for (int i = 0; i < tuple.size(); i++) {
                    results.add(tuple.getValue(i));
                }
                tuple = iterator.next();
                return !isFilterDone();
            } catch (Throwable t) {
                ServerUtil.throwIOException(region.getRegionInfo().getRegionNameAsString(), t);
                return false;
            }
        }

        @Override
        public void close() throws IOException {
            try {
                s.close();
            } finally {
                try {
                    if (iterator != null) {
                        iterator.close();
                    }
                } catch (SQLException e) {
                    ServerUtil.throwIOException(region.getRegionInfo().getRegionNameAsString(), e);
                } finally {
                    chunk.close();
                }
            }
        }
    };
}
Also used : TenantCache(org.apache.phoenix.cache.TenantCache) SQLException(java.sql.SQLException) Region(org.apache.hadoop.hbase.regionserver.Region) BaseRegionScanner(org.apache.phoenix.coprocessor.BaseRegionScanner) List(java.util.List) ArrayList(java.util.ArrayList) MemoryManager(org.apache.phoenix.memory.MemoryManager) Tuple(org.apache.phoenix.schema.tuple.Tuple) ResultTuple(org.apache.phoenix.schema.tuple.ResultTuple)

Aggregations

TenantCache (org.apache.phoenix.cache.TenantCache)4 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)3 SQLException (java.sql.SQLException)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)1 Region (org.apache.hadoop.hbase.regionserver.Region)1 IndexMetaDataCache (org.apache.phoenix.cache.IndexMetaDataCache)1 BaseRegionScanner (org.apache.phoenix.coprocessor.BaseRegionScanner)1 ServerCacheFactory (org.apache.phoenix.coprocessor.ServerCachingProtocol.ServerCacheFactory)1 AddServerCacheResponse (org.apache.phoenix.coprocessor.generated.ServerCachingProtos.AddServerCacheResponse)1 RemoveServerCacheResponse (org.apache.phoenix.coprocessor.generated.ServerCachingProtos.RemoveServerCacheResponse)1 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)1 MemoryManager (org.apache.phoenix.memory.MemoryManager)1 ResultTuple (org.apache.phoenix.schema.tuple.ResultTuple)1 Tuple (org.apache.phoenix.schema.tuple.Tuple)1 Transaction (org.apache.tephra.Transaction)1