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);
}
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);
}
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;
}
}
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();
}
}
}
};
}
Aggregations