Search in sources :

Example 11 with PSchema

use of org.apache.phoenix.parse.PSchema in project phoenix by apache.

the class MetaDataEndpointImpl method createSchema.

@Override
public void createSchema(RpcController controller, CreateSchemaRequest request, RpcCallback<MetaDataResponse> done) {
    MetaDataResponse.Builder builder = MetaDataResponse.newBuilder();
    String schemaName = null;
    try {
        List<Mutation> schemaMutations = ProtobufUtil.getMutations(request);
        schemaName = request.getSchemaName();
        Mutation m = MetaDataUtil.getPutOnlyTableHeaderRow(schemaMutations);
        byte[] lockKey = m.getRow();
        Region region = env.getRegion();
        MetaDataMutationResult result = checkSchemaKeyInRegion(lockKey, region);
        if (result != null) {
            done.run(MetaDataMutationResult.toProto(result));
            return;
        }
        List<RowLock> locks = Lists.newArrayList();
        long clientTimeStamp = MetaDataUtil.getClientTimeStamp(schemaMutations);
        try {
            acquireLock(region, lockKey, locks);
            // Get as of latest timestamp so we can detect if we have a newer schema that already exists without
            // making an additional query
            ImmutableBytesPtr cacheKey = new ImmutableBytesPtr(lockKey);
            PSchema schema = loadSchema(env, lockKey, cacheKey, clientTimeStamp, clientTimeStamp);
            if (schema != null) {
                if (schema.getTimeStamp() < clientTimeStamp) {
                    if (!isSchemaDeleted(schema)) {
                        builder.setReturnCode(MetaDataProtos.MutationCode.SCHEMA_ALREADY_EXISTS);
                        builder.setMutationTime(EnvironmentEdgeManager.currentTimeMillis());
                        builder.setSchema(PSchema.toProto(schema));
                        done.run(builder.build());
                        return;
                    }
                } else {
                    builder.setReturnCode(MetaDataProtos.MutationCode.NEWER_SCHEMA_FOUND);
                    builder.setMutationTime(EnvironmentEdgeManager.currentTimeMillis());
                    builder.setSchema(PSchema.toProto(schema));
                    done.run(builder.build());
                    return;
                }
            }
            region.mutateRowsWithLocks(schemaMutations, Collections.<byte[]>emptySet(), HConstants.NO_NONCE, HConstants.NO_NONCE);
            // Invalidate the cache - the next getSchema call will add it
            Cache<ImmutableBytesPtr, PMetaDataEntity> metaDataCache = GlobalCache.getInstance(this.env).getMetaDataCache();
            if (cacheKey != null) {
                metaDataCache.invalidate(cacheKey);
            }
            // Get timeStamp from mutations - the above method sets it if
            // it's unset
            long currentTimeStamp = MetaDataUtil.getClientTimeStamp(schemaMutations);
            builder.setReturnCode(MetaDataProtos.MutationCode.SCHEMA_NOT_FOUND);
            builder.setMutationTime(currentTimeStamp);
            done.run(builder.build());
            return;
        } finally {
            region.releaseRowLocks(locks);
        }
    } catch (Throwable t) {
        logger.error("Creating the schema" + schemaName + "failed", t);
        ProtobufUtil.setControllerException(controller, ServerUtil.createIOException(schemaName, t));
    }
}
Also used : MetaDataResponse(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) PSchema(org.apache.phoenix.parse.PSchema) ByteString(com.google.protobuf.ByteString) PMetaDataEntity(org.apache.phoenix.schema.PMetaDataEntity) Region(org.apache.hadoop.hbase.regionserver.Region) Mutation(org.apache.hadoop.hbase.client.Mutation) RowLock(org.apache.hadoop.hbase.regionserver.Region.RowLock)

Example 12 with PSchema

use of org.apache.phoenix.parse.PSchema in project phoenix by apache.

the class MetaDataEndpointImpl method doDropSchema.

private MetaDataMutationResult doDropSchema(long clientTimeStamp, String schemaName, byte[] key, List<Mutation> schemaMutations, List<ImmutableBytesPtr> invalidateList) throws IOException, SQLException {
    PSchema schema = loadSchema(env, key, new ImmutableBytesPtr(key), clientTimeStamp, clientTimeStamp);
    boolean areTablesExists = false;
    if (schema == null) {
        return new MetaDataMutationResult(MutationCode.SCHEMA_NOT_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
    }
    if (schema.getTimeStamp() < clientTimeStamp) {
        Region region = env.getRegion();
        Scan scan = MetaDataUtil.newTableRowsScan(SchemaUtil.getKeyForSchema(null, schemaName), MIN_TABLE_TIMESTAMP, clientTimeStamp);
        List<Cell> results = Lists.newArrayList();
        try (RegionScanner scanner = region.getScanner(scan)) {
            scanner.next(results);
            if (results.isEmpty()) {
                // Should not be possible
                return new MetaDataMutationResult(MutationCode.SCHEMA_NOT_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
            }
            do {
                Cell kv = results.get(0);
                if (Bytes.compareTo(kv.getRowArray(), kv.getRowOffset(), kv.getRowLength(), key, 0, key.length) != 0) {
                    areTablesExists = true;
                    break;
                }
                results.clear();
                scanner.next(results);
            } while (!results.isEmpty());
        }
        if (areTablesExists) {
            return new MetaDataMutationResult(MutationCode.TABLES_EXIST_ON_SCHEMA, schema, EnvironmentEdgeManager.currentTimeMillis());
        }
        invalidateList.add(new ImmutableBytesPtr(key));
        return new MetaDataMutationResult(MutationCode.SCHEMA_ALREADY_EXISTS, schema, EnvironmentEdgeManager.currentTimeMillis());
    }
    return new MetaDataMutationResult(MutationCode.SCHEMA_NOT_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
}
Also used : RegionScanner(org.apache.hadoop.hbase.regionserver.RegionScanner) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) PSchema(org.apache.phoenix.parse.PSchema) Region(org.apache.hadoop.hbase.regionserver.Region) Scan(org.apache.hadoop.hbase.client.Scan) Cell(org.apache.hadoop.hbase.Cell)

Example 13 with PSchema

use of org.apache.phoenix.parse.PSchema in project phoenix by apache.

the class MetaDataEndpointImpl method buildSchemas.

private List<PSchema> buildSchemas(List<byte[]> keys, Region region, long clientTimeStamp, ImmutableBytesPtr cacheKey) throws IOException, SQLException {
    List<KeyRange> keyRanges = Lists.newArrayListWithExpectedSize(keys.size());
    for (byte[] key : keys) {
        byte[] stopKey = ByteUtil.concat(key, QueryConstants.SEPARATOR_BYTE_ARRAY);
        ByteUtil.nextKey(stopKey, stopKey.length);
        keyRanges.add(PVarbinary.INSTANCE.getKeyRange(key, true, stopKey, false));
    }
    Scan scan = new Scan();
    scan.setTimeRange(MIN_TABLE_TIMESTAMP, clientTimeStamp);
    ScanRanges scanRanges = ScanRanges.createPointLookup(keyRanges);
    scanRanges.initializeScan(scan);
    scan.setFilter(scanRanges.getSkipScanFilter());
    RegionScanner scanner = region.getScanner(scan);
    Cache<ImmutableBytesPtr, PMetaDataEntity> metaDataCache = GlobalCache.getInstance(this.env).getMetaDataCache();
    List<PSchema> schemas = new ArrayList<PSchema>();
    PSchema schema = null;
    try {
        for (int i = 0; i < keys.size(); i++) {
            schema = null;
            schema = getSchema(scanner, clientTimeStamp);
            if (schema == null) {
                return null;
            }
            metaDataCache.put(cacheKey, schema);
            schemas.add(schema);
        }
        return schemas;
    } finally {
        scanner.close();
    }
}
Also used : KeyRange(org.apache.phoenix.query.KeyRange) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) ArrayList(java.util.ArrayList) PSchema(org.apache.phoenix.parse.PSchema) ScanRanges(org.apache.phoenix.compile.ScanRanges) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint) RegionScanner(org.apache.hadoop.hbase.regionserver.RegionScanner) PMetaDataEntity(org.apache.phoenix.schema.PMetaDataEntity) Scan(org.apache.hadoop.hbase.client.Scan)

Aggregations

PSchema (org.apache.phoenix.parse.PSchema)13 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)6 Scan (org.apache.hadoop.hbase.client.Scan)5 Region (org.apache.hadoop.hbase.regionserver.Region)4 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)4 SQLException (java.sql.SQLException)3 Cell (org.apache.hadoop.hbase.Cell)3 Mutation (org.apache.hadoop.hbase.client.Mutation)3 RegionScanner (org.apache.hadoop.hbase.regionserver.RegionScanner)3 MutationState (org.apache.phoenix.execute.MutationState)3 PMetaDataEntity (org.apache.phoenix.schema.PMetaDataEntity)3 ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RowLock (org.apache.hadoop.hbase.regionserver.Region.RowLock)2 MutationCode (org.apache.phoenix.coprocessor.MetaDataProtocol.MutationCode)2 MetaDataResponse (org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse)2 ResultIterator (org.apache.phoenix.iterate.ResultIterator)2 PhoenixStatement (org.apache.phoenix.jdbc.PhoenixStatement)2 PFunction (org.apache.phoenix.parse.PFunction)2