Search in sources :

Example 11 with MetaDataResponse

use of org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse in project phoenix by apache.

the class MetaDataEndpointImpl method updateIndexState.

@Override
public void updateIndexState(RpcController controller, UpdateIndexStateRequest request, RpcCallback<MetaDataResponse> done) {
    MetaDataResponse.Builder builder = MetaDataResponse.newBuilder();
    byte[] schemaName = null;
    byte[] tableName = null;
    try {
        byte[][] rowKeyMetaData = new byte[3][];
        List<Mutation> tableMetadata = ProtobufUtil.getMutations(request);
        MetaDataUtil.getTenantIdAndSchemaAndTableName(tableMetadata, rowKeyMetaData);
        byte[] tenantId = rowKeyMetaData[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
        schemaName = rowKeyMetaData[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX];
        tableName = rowKeyMetaData[PhoenixDatabaseMetaData.TABLE_NAME_INDEX];
        final byte[] key = SchemaUtil.getTableKey(tenantId, schemaName, tableName);
        Region region = env.getRegion();
        MetaDataMutationResult result = checkTableKeyInRegion(key, region);
        if (result != null) {
            done.run(MetaDataMutationResult.toProto(result));
            return;
        }
        long timeStamp = HConstants.LATEST_TIMESTAMP;
        ImmutableBytesPtr cacheKey = new ImmutableBytesPtr(key);
        List<Cell> newKVs = tableMetadata.get(0).getFamilyCellMap().get(TABLE_FAMILY_BYTES);
        Cell newKV = null;
        int disableTimeStampKVIndex = -1;
        int indexStateKVIndex = 0;
        int index = 0;
        for (Cell cell : newKVs) {
            if (Bytes.compareTo(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength(), INDEX_STATE_BYTES, 0, INDEX_STATE_BYTES.length) == 0) {
                newKV = cell;
                indexStateKVIndex = index;
                timeStamp = cell.getTimestamp();
            } else if (Bytes.compareTo(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength(), INDEX_DISABLE_TIMESTAMP_BYTES, 0, INDEX_DISABLE_TIMESTAMP_BYTES.length) == 0) {
                disableTimeStampKVIndex = index;
            }
            index++;
        }
        PIndexState newState = PIndexState.fromSerializedValue(newKV.getValueArray()[newKV.getValueOffset()]);
        RowLock rowLock = acquireLock(region, key, null);
        if (rowLock == null) {
            throw new IOException("Failed to acquire lock on " + Bytes.toStringBinary(key));
        }
        try {
            Get get = new Get(key);
            get.addColumn(TABLE_FAMILY_BYTES, DATA_TABLE_NAME_BYTES);
            get.addColumn(TABLE_FAMILY_BYTES, INDEX_STATE_BYTES);
            get.addColumn(TABLE_FAMILY_BYTES, INDEX_DISABLE_TIMESTAMP_BYTES);
            get.addColumn(TABLE_FAMILY_BYTES, ROW_KEY_ORDER_OPTIMIZABLE_BYTES);
            Result currentResult = region.get(get);
            if (currentResult.rawCells().length == 0) {
                builder.setReturnCode(MetaDataProtos.MutationCode.TABLE_NOT_FOUND);
                builder.setMutationTime(EnvironmentEdgeManager.currentTimeMillis());
                done.run(builder.build());
                return;
            }
            Cell dataTableKV = currentResult.getColumnLatestCell(TABLE_FAMILY_BYTES, DATA_TABLE_NAME_BYTES);
            Cell currentStateKV = currentResult.getColumnLatestCell(TABLE_FAMILY_BYTES, INDEX_STATE_BYTES);
            Cell currentDisableTimeStamp = currentResult.getColumnLatestCell(TABLE_FAMILY_BYTES, INDEX_DISABLE_TIMESTAMP_BYTES);
            boolean rowKeyOrderOptimizable = currentResult.getColumnLatestCell(TABLE_FAMILY_BYTES, ROW_KEY_ORDER_OPTIMIZABLE_BYTES) != null;
            // check permission on data table
            long clientTimeStamp = MetaDataUtil.getClientTimeStamp(tableMetadata);
            PTable loadedTable = getTable(env, key, new ImmutableBytesPtr(key), clientTimeStamp, clientTimeStamp, request.getClientVersion());
            if (loadedTable == null) {
                builder.setReturnCode(MetaDataProtos.MutationCode.TABLE_NOT_FOUND);
                builder.setMutationTime(EnvironmentEdgeManager.currentTimeMillis());
                done.run(builder.build());
                return;
            }
            getCoprocessorHost().preIndexUpdate(Bytes.toString(tenantId), SchemaUtil.getTableName(schemaName, tableName), TableName.valueOf(loadedTable.getPhysicalName().getBytes()), getParentPhysicalTableName(loadedTable), newState);
            PIndexState currentState = PIndexState.fromSerializedValue(currentStateKV.getValueArray()[currentStateKV.getValueOffset()]);
            // Timestamp of INDEX_STATE gets updated with each call
            long actualTimestamp = currentStateKV.getTimestamp();
            long curTimeStampVal = 0;
            long newDisableTimeStamp = 0;
            if ((currentDisableTimeStamp != null && currentDisableTimeStamp.getValueLength() > 0)) {
                curTimeStampVal = (Long) PLong.INSTANCE.toObject(currentDisableTimeStamp.getValueArray(), currentDisableTimeStamp.getValueOffset(), currentDisableTimeStamp.getValueLength());
                // new DisableTimeStamp is passed in
                if (disableTimeStampKVIndex >= 0) {
                    Cell newDisableTimeStampCell = newKVs.get(disableTimeStampKVIndex);
                    long expectedTimestamp = newDisableTimeStampCell.getTimestamp();
                    // index write failed before the rebuild was complete.
                    if (actualTimestamp > expectedTimestamp) {
                        builder.setReturnCode(MetaDataProtos.MutationCode.UNALLOWED_TABLE_MUTATION);
                        builder.setMutationTime(EnvironmentEdgeManager.currentTimeMillis());
                        done.run(builder.build());
                        return;
                    }
                    newDisableTimeStamp = (Long) PLong.INSTANCE.toObject(newDisableTimeStampCell.getValueArray(), newDisableTimeStampCell.getValueOffset(), newDisableTimeStampCell.getValueLength());
                    // index in which case the state will be INACTIVE or PENDING_ACTIVE.
                    if (curTimeStampVal != 0 && (newState == PIndexState.DISABLE || newState == PIndexState.PENDING_ACTIVE || newState == PIndexState.PENDING_DISABLE) && Math.abs(curTimeStampVal) < Math.abs(newDisableTimeStamp)) {
                        // do not reset disable timestamp as we want to keep the min
                        newKVs.remove(disableTimeStampKVIndex);
                        disableTimeStampKVIndex = -1;
                    }
                }
            }
            // Detect invalid transitions
            if (currentState == PIndexState.BUILDING) {
                if (newState == PIndexState.USABLE) {
                    builder.setReturnCode(MetaDataProtos.MutationCode.UNALLOWED_TABLE_MUTATION);
                    builder.setMutationTime(EnvironmentEdgeManager.currentTimeMillis());
                    done.run(builder.build());
                    return;
                }
            } else if (currentState == PIndexState.DISABLE) {
                // Can't transition back to INACTIVE if INDEX_DISABLE_TIMESTAMP is 0
                if (newState != PIndexState.BUILDING && newState != PIndexState.DISABLE && (newState != PIndexState.INACTIVE || curTimeStampVal == 0)) {
                    builder.setReturnCode(MetaDataProtos.MutationCode.UNALLOWED_TABLE_MUTATION);
                    builder.setMutationTime(EnvironmentEdgeManager.currentTimeMillis());
                    done.run(builder.build());
                    return;
                }
                // Done building, but was disable before that, so that in disabled state
                if (newState == PIndexState.ACTIVE) {
                    newState = PIndexState.DISABLE;
                }
                // Can't transition from DISABLE to PENDING_DISABLE
                if (newState == PIndexState.PENDING_DISABLE) {
                    builder.setReturnCode(MetaDataProtos.MutationCode.UNALLOWED_TABLE_MUTATION);
                    builder.setMutationTime(EnvironmentEdgeManager.currentTimeMillis());
                    done.run(builder.build());
                    return;
                }
            }
            if (currentState == PIndexState.BUILDING && newState != PIndexState.ACTIVE) {
                timeStamp = currentStateKV.getTimestamp();
            }
            if ((currentState == PIndexState.ACTIVE || currentState == PIndexState.PENDING_ACTIVE) && newState == PIndexState.UNUSABLE) {
                newState = PIndexState.INACTIVE;
                newKVs.set(indexStateKVIndex, KeyValueUtil.newKeyValue(key, TABLE_FAMILY_BYTES, INDEX_STATE_BYTES, timeStamp, Bytes.toBytes(newState.getSerializedValue())));
            } else if ((currentState == PIndexState.INACTIVE || currentState == PIndexState.PENDING_ACTIVE) && newState == PIndexState.USABLE) {
                // Don't allow manual state change to USABLE (i.e. ACTIVE) if non zero INDEX_DISABLE_TIMESTAMP
                if (curTimeStampVal != 0) {
                    newState = currentState;
                } else {
                    newState = PIndexState.ACTIVE;
                }
                newKVs.set(indexStateKVIndex, KeyValueUtil.newKeyValue(key, TABLE_FAMILY_BYTES, INDEX_STATE_BYTES, timeStamp, Bytes.toBytes(newState.getSerializedValue())));
            }
            PTable returnTable = null;
            if (currentState != newState || disableTimeStampKVIndex != -1) {
                // make a copy of tableMetadata so we can add to it
                tableMetadata = new ArrayList<Mutation>(tableMetadata);
                // Always include the empty column value at latest timestamp so
                // that clients pull over update.
                Put emptyValue = new Put(key);
                emptyValue.addColumn(TABLE_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES, HConstants.LATEST_TIMESTAMP, QueryConstants.EMPTY_COLUMN_VALUE_BYTES);
                tableMetadata.add(emptyValue);
                byte[] dataTableKey = null;
                if (dataTableKV != null) {
                    dataTableKey = SchemaUtil.getTableKey(tenantId, schemaName, CellUtil.cloneValue(dataTableKV));
                    // insert an empty KV to trigger time stamp update on data table row
                    Put p = new Put(dataTableKey);
                    p.addColumn(TABLE_FAMILY_BYTES, QueryConstants.EMPTY_COLUMN_BYTES, HConstants.LATEST_TIMESTAMP, QueryConstants.EMPTY_COLUMN_VALUE_BYTES);
                    tableMetadata.add(p);
                }
                boolean setRowKeyOrderOptimizableCell = newState == PIndexState.BUILDING && !rowKeyOrderOptimizable;
                // so that the row keys get generated using the new row key format
                if (setRowKeyOrderOptimizableCell) {
                    UpgradeUtil.addRowKeyOrderOptimizableCell(tableMetadata, key, timeStamp);
                }
                mutateRowsWithLocks(region, tableMetadata, Collections.<byte[]>emptySet(), HConstants.NO_NONCE, HConstants.NO_NONCE);
                // Invalidate from cache
                Cache<ImmutableBytesPtr, PMetaDataEntity> metaDataCache = GlobalCache.getInstance(this.env).getMetaDataCache();
                metaDataCache.invalidate(cacheKey);
                if (dataTableKey != null) {
                    metaDataCache.invalidate(new ImmutableBytesPtr(dataTableKey));
                }
                if (setRowKeyOrderOptimizableCell || disableTimeStampKVIndex != -1 || currentState == PIndexState.DISABLE || newState == PIndexState.BUILDING) {
                    returnTable = doGetTable(key, HConstants.LATEST_TIMESTAMP, rowLock, request.getClientVersion());
                }
            }
            // Get client timeStamp from mutations, since it may get updated by the
            // mutateRowsWithLocks call
            long currentTime = MetaDataUtil.getClientTimeStamp(tableMetadata);
            builder.setReturnCode(MetaDataProtos.MutationCode.TABLE_ALREADY_EXISTS);
            builder.setMutationTime(currentTime);
            if (returnTable != null) {
                builder.setTable(PTableImpl.toProto(returnTable));
            }
            done.run(builder.build());
            return;
        } finally {
            rowLock.release();
        }
    } catch (Throwable t) {
        logger.error("updateIndexState failed", t);
        ProtobufUtil.setControllerException(controller, ServerUtil.createIOException(SchemaUtil.getTableName(schemaName, tableName), t));
    }
}
Also used : PTable(org.apache.phoenix.schema.PTable) Result(org.apache.hadoop.hbase.client.Result) PMetaDataEntity(org.apache.phoenix.schema.PMetaDataEntity) Cell(org.apache.hadoop.hbase.Cell) RowLock(org.apache.hadoop.hbase.regionserver.Region.RowLock) MetaDataResponse(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse) PIndexState(org.apache.phoenix.schema.PIndexState) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) IOException(java.io.IOException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint) Put(org.apache.hadoop.hbase.client.Put) Get(org.apache.hadoop.hbase.client.Get) Region(org.apache.hadoop.hbase.regionserver.Region) Mutation(org.apache.hadoop.hbase.client.Mutation)

Example 12 with MetaDataResponse

use of org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse in project phoenix by apache.

the class MetaDataEndpointImpl method dropTable.

@Override
public void dropTable(RpcController controller, DropTableRequest request, RpcCallback<MetaDataResponse> done) {
    MetaDataResponse.Builder builder = MetaDataResponse.newBuilder();
    boolean isCascade = request.getCascade();
    byte[][] rowKeyMetaData = new byte[3][];
    String tableType = request.getTableType();
    byte[] schemaName = null;
    byte[] tableName = null;
    try {
        List<Mutation> tableMetadata = ProtobufUtil.getMutations(request);
        MetaDataUtil.getTenantIdAndSchemaAndTableName(tableMetadata, rowKeyMetaData);
        byte[] tenantIdBytes = rowKeyMetaData[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
        schemaName = rowKeyMetaData[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX];
        tableName = rowKeyMetaData[PhoenixDatabaseMetaData.TABLE_NAME_INDEX];
        // Disallow deletion of a system table
        if (tableType.equals(PTableType.SYSTEM.getSerializedValue())) {
            builder.setReturnCode(MetaDataProtos.MutationCode.UNALLOWED_TABLE_MUTATION);
            builder.setMutationTime(EnvironmentEdgeManager.currentTimeMillis());
            done.run(builder.build());
            return;
        }
        List<byte[]> tableNamesToDelete = Lists.newArrayList();
        List<SharedTableState> sharedTablesToDelete = Lists.newArrayList();
        byte[] parentTableName = MetaDataUtil.getParentTableName(tableMetadata);
        byte[] lockTableName = parentTableName == null ? tableName : parentTableName;
        byte[] lockKey = SchemaUtil.getTableKey(tenantIdBytes, schemaName, lockTableName);
        byte[] key = parentTableName == null ? lockKey : SchemaUtil.getTableKey(tenantIdBytes, schemaName, tableName);
        Region region = env.getRegion();
        MetaDataMutationResult result = checkTableKeyInRegion(key, region);
        if (result != null) {
            done.run(MetaDataMutationResult.toProto(result));
            return;
        }
        PTableType ptableType = PTableType.fromSerializedValue(tableType);
        long clientTimeStamp = MetaDataUtil.getClientTimeStamp(tableMetadata);
        byte[] cKey = SchemaUtil.getTableKey(tenantIdBytes, schemaName, tableName);
        PTable loadedTable = getTable(env, cKey, new ImmutableBytesPtr(cKey), clientTimeStamp, clientTimeStamp, request.getClientVersion());
        if (loadedTable == null) {
            builder.setReturnCode(MetaDataProtos.MutationCode.TABLE_NOT_FOUND);
            builder.setMutationTime(EnvironmentEdgeManager.currentTimeMillis());
            done.run(builder.build());
            return;
        }
        getCoprocessorHost().preDropTable(Bytes.toString(tenantIdBytes), SchemaUtil.getTableName(schemaName, tableName), TableName.valueOf(loadedTable.getPhysicalName().getBytes()), getParentPhysicalTableName(loadedTable), ptableType, loadedTable.getIndexes());
        List<RowLock> locks = Lists.newArrayList();
        try {
            acquireLock(region, lockKey, locks);
            if (key != lockKey) {
                acquireLock(region, key, locks);
            }
            List<ImmutableBytesPtr> invalidateList = new ArrayList<ImmutableBytesPtr>();
            result = doDropTable(key, tenantIdBytes, schemaName, tableName, parentTableName, PTableType.fromSerializedValue(tableType), tableMetadata, invalidateList, locks, tableNamesToDelete, sharedTablesToDelete, isCascade, request.getClientVersion());
            if (result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS) {
                done.run(MetaDataMutationResult.toProto(result));
                return;
            }
            Cache<ImmutableBytesPtr, PMetaDataEntity> metaDataCache = GlobalCache.getInstance(this.env).getMetaDataCache();
            // Commit the list of deletion.
            mutateRowsWithLocks(region, tableMetadata, Collections.<byte[]>emptySet(), HConstants.NO_NONCE, HConstants.NO_NONCE);
            long currentTime = MetaDataUtil.getClientTimeStamp(tableMetadata);
            for (ImmutableBytesPtr ckey : invalidateList) {
                metaDataCache.put(ckey, newDeletedTableMarker(currentTime));
            }
            if (parentTableName != null) {
                ImmutableBytesPtr parentCacheKey = new ImmutableBytesPtr(lockKey);
                metaDataCache.invalidate(parentCacheKey);
            }
            done.run(MetaDataMutationResult.toProto(result));
            return;
        } finally {
            releaseRowLocks(region, locks);
        }
    } catch (Throwable t) {
        logger.error("dropTable failed", t);
        ProtobufUtil.setControllerException(controller, ServerUtil.createIOException(SchemaUtil.getTableName(schemaName, tableName), t));
    }
}
Also used : MetaDataResponse(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse) PTableType(org.apache.phoenix.schema.PTableType) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString) PTable(org.apache.phoenix.schema.PTable) 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 13 with MetaDataResponse

use of org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse in project phoenix by apache.

the class MetaDataEndpointImpl method getSchema.

@Override
public void getSchema(RpcController controller, GetSchemaRequest request, RpcCallback<MetaDataResponse> done) {
    MetaDataResponse.Builder builder = MetaDataResponse.newBuilder();
    Region region = env.getRegion();
    String schemaName = request.getSchemaName();
    byte[] lockKey = SchemaUtil.getSchemaKey(schemaName);
    MetaDataMutationResult result = checkSchemaKeyInRegion(lockKey, region);
    if (result != null) {
        done.run(MetaDataMutationResult.toProto(result));
        return;
    }
    long clientTimeStamp = request.getClientTimestamp();
    List<RowLock> locks = Lists.newArrayList();
    try {
        getCoprocessorHost().preGetSchema(schemaName);
        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;
                }
            }
        }
    } catch (Exception e) {
        long currentTime = EnvironmentEdgeManager.currentTimeMillis();
        builder.setReturnCode(MetaDataProtos.MutationCode.SCHEMA_NOT_FOUND);
        builder.setMutationTime(currentTime);
        done.run(builder.build());
        return;
    } finally {
        releaseRowLocks(region, locks);
    }
}
Also used : MetaDataResponse(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) Region(org.apache.hadoop.hbase.regionserver.Region) PSchema(org.apache.phoenix.parse.PSchema) ByteString(com.google.protobuf.ByteString) CoprocessorException(org.apache.hadoop.hbase.coprocessor.CoprocessorException) SequenceNotFoundException(org.apache.phoenix.schema.SequenceNotFoundException) IOException(java.io.IOException) ColumnNotFoundException(org.apache.phoenix.schema.ColumnNotFoundException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) ColumnFamilyNotFoundException(org.apache.phoenix.schema.ColumnFamilyNotFoundException) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) SQLException(java.sql.SQLException) SequenceAlreadyExistsException(org.apache.phoenix.schema.SequenceAlreadyExistsException) RowLock(org.apache.hadoop.hbase.regionserver.Region.RowLock)

Example 14 with MetaDataResponse

use of org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse in project phoenix by apache.

the class MetaDataEndpointImpl method getTable.

@Override
public void getTable(RpcController controller, GetTableRequest request, RpcCallback<MetaDataResponse> done) {
    MetaDataResponse.Builder builder = MetaDataResponse.newBuilder();
    byte[] tenantId = request.getTenantId().toByteArray();
    byte[] schemaName = request.getSchemaName().toByteArray();
    byte[] tableName = request.getTableName().toByteArray();
    byte[] key = SchemaUtil.getTableKey(tenantId, schemaName, tableName);
    long tableTimeStamp = request.getTableTimestamp();
    try {
        // TODO: check that key is within region.getStartKey() and region.getEndKey()
        // and return special code to force client to lookup region from meta.
        Region region = env.getRegion();
        MetaDataMutationResult result = checkTableKeyInRegion(key, region);
        if (result != null) {
            done.run(MetaDataMutationResult.toProto(result));
            return;
        }
        long currentTime = EnvironmentEdgeManager.currentTimeMillis();
        PTable table = doGetTable(key, request.getClientTimestamp(), request.getClientVersion());
        if (table == null) {
            builder.setReturnCode(MetaDataProtos.MutationCode.TABLE_NOT_FOUND);
            builder.setMutationTime(currentTime);
            done.run(builder.build());
            return;
        }
        getCoprocessorHost().preGetTable(Bytes.toString(tenantId), SchemaUtil.getTableName(schemaName, tableName), TableName.valueOf(table.getPhysicalName().getBytes()));
        builder.setReturnCode(MetaDataProtos.MutationCode.TABLE_ALREADY_EXISTS);
        builder.setMutationTime(currentTime);
        if (blockWriteRebuildIndex) {
            long disableIndexTimestamp = table.getIndexDisableTimestamp();
            long minNonZerodisableIndexTimestamp = disableIndexTimestamp > 0 ? disableIndexTimestamp : Long.MAX_VALUE;
            for (PTable index : table.getIndexes()) {
                disableIndexTimestamp = index.getIndexDisableTimestamp();
                if (disableIndexTimestamp > 0 && (index.getIndexState() == PIndexState.ACTIVE || index.getIndexState() == PIndexState.PENDING_ACTIVE || index.getIndexState() == PIndexState.PENDING_DISABLE) && disableIndexTimestamp < minNonZerodisableIndexTimestamp) {
                    minNonZerodisableIndexTimestamp = disableIndexTimestamp;
                }
            }
            // batch applied to it.
            if (minNonZerodisableIndexTimestamp != Long.MAX_VALUE) {
                // Subtract one because we add one due to timestamp granularity in Windows
                builder.setMutationTime(minNonZerodisableIndexTimestamp - 1);
            }
        }
        if (table.getTimeStamp() != tableTimeStamp) {
            builder.setTable(PTableImpl.toProto(table));
        }
        done.run(builder.build());
        return;
    } catch (Throwable t) {
        logger.error("getTable failed", t);
        ProtobufUtil.setControllerException(controller, ServerUtil.createIOException(SchemaUtil.getTableName(schemaName, tableName), t));
    }
}
Also used : MetaDataResponse(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse) Region(org.apache.hadoop.hbase.regionserver.Region) PTable(org.apache.phoenix.schema.PTable)

Example 15 with MetaDataResponse

use of org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse 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;
                }
            }
            mutateRowsWithLocks(region, 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 {
            releaseRowLocks(region, 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)

Aggregations

MetaDataResponse (org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse)21 Mutation (org.apache.hadoop.hbase.client.Mutation)16 IOException (java.io.IOException)14 BlockingRpcCallback (org.apache.hadoop.hbase.ipc.BlockingRpcCallback)12 ServerRpcController (org.apache.hadoop.hbase.ipc.ServerRpcController)12 MetaDataService (org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataService)12 KeyValueBuilder (org.apache.phoenix.hbase.index.util.KeyValueBuilder)12 Batch (org.apache.hadoop.hbase.client.coprocessor.Batch)11 MutationProto (org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto)11 PhoenixIOException (org.apache.phoenix.exception.PhoenixIOException)10 NonTxIndexBuilder (org.apache.phoenix.hbase.index.covered.NonTxIndexBuilder)9 PhoenixIndexBuilder (org.apache.phoenix.index.PhoenixIndexBuilder)9 Region (org.apache.hadoop.hbase.regionserver.Region)8 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)8 RowLock (org.apache.hadoop.hbase.regionserver.Region.RowLock)6 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)6 ByteString (com.google.protobuf.ByteString)5 ArrayList (java.util.ArrayList)5 PMetaDataEntity (org.apache.phoenix.schema.PMetaDataEntity)5 PTable (org.apache.phoenix.schema.PTable)5