Search in sources :

Example 31 with MetaDataMutationResult

use of org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult in project phoenix by apache.

the class MutationState method validate.

private long validate(TableRef tableRef, Map<ImmutableBytesPtr, RowMutationState> rowKeyToColumnMap) throws SQLException {
    Long scn = connection.getSCN();
    MetaDataClient client = new MetaDataClient(connection);
    long serverTimeStamp = tableRef.getTimeStamp();
    // If we're auto committing, we've already validated the schema when we got the ColumnResolver,
    // so no need to do it again here.
    PTable table = tableRef.getTable();
    MetaDataMutationResult result = client.updateCache(table.getSchemaName().getString(), table.getTableName().getString());
    PTable resolvedTable = result.getTable();
    if (resolvedTable == null) {
        throw new TableNotFoundException(table.getSchemaName().getString(), table.getTableName().getString());
    }
    // Always update tableRef table as the one we've cached may be out of date since when we executed
    // the UPSERT VALUES call and updated in the cache before this.
    tableRef.setTable(resolvedTable);
    List<PTable> indexes = resolvedTable.getIndexes();
    for (PTable idxTtable : indexes) {
        // our failure mode is block writes on index failure.
        if (idxTtable.getIndexState() == PIndexState.ACTIVE && idxTtable.getIndexDisableTimestamp() > 0) {
            throw new SQLExceptionInfo.Builder(SQLExceptionCode.INDEX_FAILURE_BLOCK_WRITE).setSchemaName(table.getSchemaName().getString()).setTableName(table.getTableName().getString()).build().buildException();
        }
    }
    long timestamp = result.getMutationTime();
    if (timestamp != QueryConstants.UNSET_TIMESTAMP) {
        serverTimeStamp = timestamp;
        if (result.wasUpdated()) {
            List<PColumn> columns = Lists.newArrayListWithExpectedSize(table.getColumns().size());
            for (Map.Entry<ImmutableBytesPtr, RowMutationState> rowEntry : rowKeyToColumnMap.entrySet()) {
                RowMutationState valueEntry = rowEntry.getValue();
                if (valueEntry != null) {
                    Map<PColumn, byte[]> colValues = valueEntry.getColumnValues();
                    if (colValues != PRow.DELETE_MARKER) {
                        for (PColumn column : colValues.keySet()) {
                            if (!column.isDynamic())
                                columns.add(column);
                        }
                    }
                }
            }
            for (PColumn column : columns) {
                if (column != null) {
                    resolvedTable.getColumnFamily(column.getFamilyName().getString()).getPColumnForColumnName(column.getName().getString());
                }
            }
        }
    }
    return scn == null ? serverTimeStamp == QueryConstants.UNSET_TIMESTAMP ? HConstants.LATEST_TIMESTAMP : serverTimeStamp : scn;
}
Also used : MetaDataClient(org.apache.phoenix.schema.MetaDataClient) PhoenixIndexBuilder(org.apache.phoenix.index.PhoenixIndexBuilder) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) PTable(org.apache.phoenix.schema.PTable) PColumn(org.apache.phoenix.schema.PColumn) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PLong(org.apache.phoenix.schema.types.PLong) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult) Map(java.util.Map)

Example 32 with MetaDataMutationResult

use of org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult in project phoenix by apache.

the class ConnectionQueryServicesImpl method getTable.

private PTable getTable(PName tenantId, String fullTableName, long timestamp) throws SQLException {
    PTable table;
    try {
        PMetaData metadata = latestMetaData;
        throwConnectionClosedIfNullMetaData();
        table = metadata.getTableRef(new PTableKey(tenantId, fullTableName)).getTable();
        if (table.getTimeStamp() >= timestamp) {
            // the case
            throw new TableNotFoundException(table.getSchemaName().getString(), table.getTableName().getString());
        }
    } catch (TableNotFoundException e) {
        byte[] schemaName = Bytes.toBytes(SchemaUtil.getSchemaNameFromFullName(fullTableName));
        byte[] tableName = Bytes.toBytes(SchemaUtil.getTableNameFromFullName(fullTableName));
        MetaDataMutationResult result = this.getTable(tenantId, schemaName, tableName, HConstants.LATEST_TIMESTAMP, timestamp);
        table = result.getTable();
        if (table == null) {
            throw e;
        }
    }
    return table;
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PMetaData(org.apache.phoenix.schema.PMetaData) PTableKey(org.apache.phoenix.schema.PTableKey) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult) PTable(org.apache.phoenix.schema.PTable)

Example 33 with MetaDataMutationResult

use of org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult in project phoenix by apache.

the class ConnectionQueryServicesImpl method addColumn.

@Override
public MetaDataMutationResult addColumn(final List<Mutation> tableMetaData, PTable table, Map<String, List<Pair<String, Object>>> stmtProperties, Set<String> colFamiliesForPColumnsToBeAdded, List<PColumn> columns) throws SQLException {
    List<Pair<byte[], Map<String, Object>>> families = new ArrayList<>(stmtProperties.size());
    Map<String, Object> tableProps = new HashMap<String, Object>();
    Set<HTableDescriptor> tableDescriptors = Collections.emptySet();
    Set<HTableDescriptor> origTableDescriptors = Collections.emptySet();
    boolean nonTxToTx = false;
    Pair<HTableDescriptor, HTableDescriptor> tableDescriptorPair = separateAndValidateProperties(table, stmtProperties, colFamiliesForPColumnsToBeAdded, families, tableProps);
    HTableDescriptor tableDescriptor = tableDescriptorPair.getSecond();
    HTableDescriptor origTableDescriptor = tableDescriptorPair.getFirst();
    if (tableDescriptor != null) {
        tableDescriptors = Sets.newHashSetWithExpectedSize(3 + table.getIndexes().size());
        origTableDescriptors = Sets.newHashSetWithExpectedSize(3 + table.getIndexes().size());
        tableDescriptors.add(tableDescriptor);
        origTableDescriptors.add(origTableDescriptor);
        nonTxToTx = Boolean.TRUE.equals(tableProps.get(TxConstants.READ_NON_TX_DATA));
        /*
             * If the table was transitioned from non transactional to transactional, we need
             * to also transition the index tables.
             */
        if (nonTxToTx) {
            updateDescriptorForTx(table, tableProps, tableDescriptor, Boolean.TRUE.toString(), tableDescriptors, origTableDescriptors);
        }
    }
    boolean success = false;
    boolean metaDataUpdated = !tableDescriptors.isEmpty();
    boolean pollingNeeded = !(!tableProps.isEmpty() && families.isEmpty() && colFamiliesForPColumnsToBeAdded.isEmpty());
    MetaDataMutationResult result = null;
    try {
        boolean modifyHTable = true;
        if (table.getType() == PTableType.VIEW) {
            boolean canViewsAddNewCF = props.getBoolean(QueryServices.ALLOW_VIEWS_ADD_NEW_CF_BASE_TABLE, QueryServicesOptions.DEFAULT_ALLOW_VIEWS_ADD_NEW_CF_BASE_TABLE);
            // When adding a column to a view, base physical table should only be modified when new column families are being added.
            modifyHTable = canViewsAddNewCF && !existingColumnFamiliesForBaseTable(table.getPhysicalName()).containsAll(colFamiliesForPColumnsToBeAdded);
        }
        if (modifyHTable) {
            sendHBaseMetaData(tableDescriptors, pollingNeeded);
        }
        // Also, could be used to update property values on ALTER TABLE t SET prop=xxx
        if ((tableMetaData.isEmpty()) || (tableMetaData.size() == 1 && tableMetaData.get(0).isEmpty())) {
            return new MetaDataMutationResult(MutationCode.NO_OP, System.currentTimeMillis(), table);
        }
        byte[][] rowKeyMetaData = new byte[3][];
        PTableType tableType = table.getType();
        Mutation m = tableMetaData.get(0);
        byte[] rowKey = m.getRow();
        SchemaUtil.getVarChars(rowKey, rowKeyMetaData);
        byte[] tenantIdBytes = rowKeyMetaData[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
        byte[] schemaBytes = rowKeyMetaData[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX];
        byte[] tableBytes = rowKeyMetaData[PhoenixDatabaseMetaData.TABLE_NAME_INDEX];
        byte[] tableKey = SchemaUtil.getTableKey(tenantIdBytes, schemaBytes, tableBytes);
        ImmutableBytesWritable ptr = new ImmutableBytesWritable();
        result = metaDataCoprocessorExec(tableKey, new Batch.Call<MetaDataService, MetaDataResponse>() {

            @Override
            public MetaDataResponse call(MetaDataService instance) throws IOException {
                ServerRpcController controller = new ServerRpcController();
                BlockingRpcCallback<MetaDataResponse> rpcCallback = new BlockingRpcCallback<MetaDataResponse>();
                AddColumnRequest.Builder builder = AddColumnRequest.newBuilder();
                for (Mutation m : tableMetaData) {
                    MutationProto mp = ProtobufUtil.toProto(m);
                    builder.addTableMetadataMutations(mp.toByteString());
                }
                builder.setClientVersion(VersionUtil.encodeVersion(PHOENIX_MAJOR_VERSION, PHOENIX_MINOR_VERSION, PHOENIX_PATCH_NUMBER));
                instance.addColumn(controller, builder.build(), rpcCallback);
                if (controller.getFailedOn() != null) {
                    throw controller.getFailedOn();
                }
                return rpcCallback.get();
            }
        });
        if (result.getMutationCode() == MutationCode.COLUMN_NOT_FOUND || result.getMutationCode() == MutationCode.TABLE_ALREADY_EXISTS) {
            // Success
            success = true;
            // Flush the table if transitioning DISABLE_WAL from TRUE to FALSE
            if (MetaDataUtil.getMutationValue(m, PhoenixDatabaseMetaData.DISABLE_WAL_BYTES, kvBuilder, ptr) && Boolean.FALSE.equals(PBoolean.INSTANCE.toObject(ptr))) {
                flushTable(table.getPhysicalName().getBytes());
            }
            if (tableType == PTableType.TABLE) {
                // If we're changing MULTI_TENANT to true or false, create or drop the view index table
                if (MetaDataUtil.getMutationValue(m, PhoenixDatabaseMetaData.MULTI_TENANT_BYTES, kvBuilder, ptr)) {
                    long timestamp = MetaDataUtil.getClientTimeStamp(m);
                    if (Boolean.TRUE.equals(PBoolean.INSTANCE.toObject(ptr.get(), ptr.getOffset(), ptr.getLength()))) {
                        this.ensureViewIndexTableCreated(table, timestamp, table.isNamespaceMapped());
                    } else {
                        this.ensureViewIndexTableDropped(table.getPhysicalName().getBytes(), timestamp);
                    }
                }
            }
        }
    } finally {
        // Note that if this fails, we're in a corrupt state.
        if (!success && metaDataUpdated && nonTxToTx) {
            sendHBaseMetaData(origTableDescriptors, pollingNeeded);
        }
    }
    return result;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ServerRpcController(org.apache.hadoop.hbase.ipc.ServerRpcController) MutationProto(org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto) MetaDataService(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataService) BlockingRpcCallback(org.apache.hadoop.hbase.ipc.BlockingRpcCallback) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult) Pair(org.apache.hadoop.hbase.util.Pair) ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) MetaDataResponse(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse) PTableType(org.apache.phoenix.schema.PTableType) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) AddColumnRequest(org.apache.phoenix.coprocessor.generated.MetaDataProtos.AddColumnRequest) Mutation(org.apache.hadoop.hbase.client.Mutation)

Example 34 with MetaDataMutationResult

use of org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult in project phoenix by apache.

the class ConnectionQueryServicesImpl method createTable.

@Override
public MetaDataMutationResult createTable(final List<Mutation> tableMetaData, final byte[] physicalTableName, PTableType tableType, Map<String, Object> tableProps, final List<Pair<byte[], Map<String, Object>>> families, byte[][] splits, boolean isNamespaceMapped, final boolean allocateIndexId) throws SQLException {
    byte[][] rowKeyMetadata = new byte[3][];
    Mutation m = MetaDataUtil.getPutOnlyTableHeaderRow(tableMetaData);
    byte[] key = m.getRow();
    SchemaUtil.getVarChars(key, rowKeyMetadata);
    byte[] tenantIdBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
    byte[] schemaBytes = rowKeyMetadata[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX];
    byte[] tableBytes = rowKeyMetadata[PhoenixDatabaseMetaData.TABLE_NAME_INDEX];
    byte[] tableName = physicalTableName != null ? physicalTableName : SchemaUtil.getTableNameAsBytes(schemaBytes, tableBytes);
    boolean localIndexTable = false;
    for (Pair<byte[], Map<String, Object>> family : families) {
        if (Bytes.toString(family.getFirst()).startsWith(QueryConstants.LOCAL_INDEX_COLUMN_FAMILY_PREFIX)) {
            localIndexTable = true;
            break;
        }
    }
    if ((tableType == PTableType.VIEW && physicalTableName != null) || (tableType != PTableType.VIEW && (physicalTableName == null || localIndexTable))) {
        // For views this will ensure that metadata already exists
        // For tables and indexes, this will create the metadata if it doesn't already exist
        ensureTableCreated(tableName, tableType, tableProps, families, splits, true, isNamespaceMapped);
    }
    ImmutableBytesWritable ptr = new ImmutableBytesWritable();
    if (tableType == PTableType.INDEX) {
        // TODO: if viewIndexId is Short.MIN_VALUE, then we don't need to attempt to create it
        if (physicalTableName != null) {
            if (!localIndexTable && !MetaDataUtil.isMultiTenant(m, kvBuilder, ptr)) {
                ensureViewIndexTableCreated(tenantIdBytes.length == 0 ? null : PNameFactory.newName(tenantIdBytes), physicalTableName, MetaDataUtil.getClientTimeStamp(m), isNamespaceMapped);
            }
        }
    } else if (tableType == PTableType.TABLE && MetaDataUtil.isMultiTenant(m, kvBuilder, ptr)) {
        // Create view index table up front for multi tenant tables
        ptr.set(QueryConstants.DEFAULT_COLUMN_FAMILY_BYTES);
        MetaDataUtil.getMutationValue(m, PhoenixDatabaseMetaData.DEFAULT_COLUMN_FAMILY_NAME_BYTES, kvBuilder, ptr);
        List<Pair<byte[], Map<String, Object>>> familiesPlusDefault = null;
        for (Pair<byte[], Map<String, Object>> family : families) {
            byte[] cf = family.getFirst();
            if (Bytes.compareTo(cf, 0, cf.length, ptr.get(), ptr.getOffset(), ptr.getLength()) == 0) {
                familiesPlusDefault = families;
                break;
            }
        }
        // Don't override if default family already present
        if (familiesPlusDefault == null) {
            byte[] defaultCF = ByteUtil.copyKeyBytesIfNecessary(ptr);
            // Only use splits if table is salted, otherwise it may not be applicable
            // Always add default column family, as we don't know in advance if we'll need it
            familiesPlusDefault = Lists.newArrayList(families);
            familiesPlusDefault.add(new Pair<byte[], Map<String, Object>>(defaultCF, Collections.<String, Object>emptyMap()));
        }
        ensureViewIndexTableCreated(SchemaUtil.getPhysicalHBaseTableName(tableName, isNamespaceMapped, tableType).getBytes(), tableProps, familiesPlusDefault, MetaDataUtil.isSalted(m, kvBuilder, ptr) ? splits : null, MetaDataUtil.getClientTimeStamp(m), isNamespaceMapped);
    }
    byte[] tableKey = SchemaUtil.getTableKey(tenantIdBytes, schemaBytes, tableBytes);
    MetaDataMutationResult result = metaDataCoprocessorExec(tableKey, new Batch.Call<MetaDataService, MetaDataResponse>() {

        @Override
        public MetaDataResponse call(MetaDataService instance) throws IOException {
            ServerRpcController controller = new ServerRpcController();
            BlockingRpcCallback<MetaDataResponse> rpcCallback = new BlockingRpcCallback<MetaDataResponse>();
            CreateTableRequest.Builder builder = CreateTableRequest.newBuilder();
            for (Mutation m : tableMetaData) {
                MutationProto mp = ProtobufUtil.toProto(m);
                builder.addTableMetadataMutations(mp.toByteString());
            }
            builder.setClientVersion(VersionUtil.encodeVersion(PHOENIX_MAJOR_VERSION, PHOENIX_MINOR_VERSION, PHOENIX_PATCH_NUMBER));
            if (allocateIndexId) {
                builder.setAllocateIndexId(allocateIndexId);
            }
            CreateTableRequest build = builder.build();
            instance.createTable(controller, build, rpcCallback);
            if (controller.getFailedOn() != null) {
                throw controller.getFailedOn();
            }
            return rpcCallback.get();
        }
    });
    return result;
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) MetaDataResponse(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse) KeyValueBuilder(org.apache.phoenix.hbase.index.util.KeyValueBuilder) NonTxIndexBuilder(org.apache.phoenix.hbase.index.covered.NonTxIndexBuilder) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) PhoenixIndexBuilder(org.apache.phoenix.index.PhoenixIndexBuilder) IOException(java.io.IOException) PhoenixIOException(org.apache.phoenix.exception.PhoenixIOException) ServerRpcController(org.apache.hadoop.hbase.ipc.ServerRpcController) MutationProto(org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto) MetaDataService(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataService) Batch(org.apache.hadoop.hbase.client.coprocessor.Batch) BlockingRpcCallback(org.apache.hadoop.hbase.ipc.BlockingRpcCallback) ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) Mutation(org.apache.hadoop.hbase.client.Mutation) Map(java.util.Map) TreeMap(java.util.TreeMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult) CreateTableRequest(org.apache.phoenix.coprocessor.generated.MetaDataProtos.CreateTableRequest) Pair(org.apache.hadoop.hbase.util.Pair)

Example 35 with MetaDataMutationResult

use of org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult in project phoenix by apache.

the class ConnectionQueryServicesImpl method createSchema.

@Override
public MetaDataMutationResult createSchema(final List<Mutation> schemaMutations, final String schemaName) throws SQLException {
    ensureNamespaceCreated(schemaName);
    Mutation m = MetaDataUtil.getPutOnlyTableHeaderRow(schemaMutations);
    byte[] key = m.getRow();
    MetaDataMutationResult result = metaDataCoprocessorExec(key, new Batch.Call<MetaDataService, MetaDataResponse>() {

        @Override
        public MetaDataResponse call(MetaDataService instance) throws IOException {
            ServerRpcController controller = new ServerRpcController();
            BlockingRpcCallback<MetaDataResponse> rpcCallback = new BlockingRpcCallback<MetaDataResponse>();
            CreateSchemaRequest.Builder builder = CreateSchemaRequest.newBuilder();
            for (Mutation m : schemaMutations) {
                MutationProto mp = ProtobufUtil.toProto(m);
                builder.addTableMetadataMutations(mp.toByteString());
            }
            builder.setSchemaName(schemaName);
            builder.setClientVersion(VersionUtil.encodeVersion(PHOENIX_MAJOR_VERSION, PHOENIX_MINOR_VERSION, PHOENIX_PATCH_NUMBER));
            instance.createSchema(controller, builder.build(), rpcCallback);
            if (controller.getFailedOn() != null) {
                throw controller.getFailedOn();
            }
            return rpcCallback.get();
        }
    });
    return result;
}
Also used : MetaDataResponse(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse) KeyValueBuilder(org.apache.phoenix.hbase.index.util.KeyValueBuilder) NonTxIndexBuilder(org.apache.phoenix.hbase.index.covered.NonTxIndexBuilder) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) PhoenixIndexBuilder(org.apache.phoenix.index.PhoenixIndexBuilder) IOException(java.io.IOException) PhoenixIOException(org.apache.phoenix.exception.PhoenixIOException) ServerRpcController(org.apache.hadoop.hbase.ipc.ServerRpcController) MutationProto(org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto) MetaDataService(org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataService) Batch(org.apache.hadoop.hbase.client.coprocessor.Batch) BlockingRpcCallback(org.apache.hadoop.hbase.ipc.BlockingRpcCallback) Mutation(org.apache.hadoop.hbase.client.Mutation) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)

Aggregations

MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)36 Mutation (org.apache.hadoop.hbase.client.Mutation)20 MutationCode (org.apache.phoenix.coprocessor.MetaDataProtocol.MutationCode)15 PLong (org.apache.phoenix.schema.types.PLong)11 MutationState (org.apache.phoenix.execute.MutationState)10 PTable (org.apache.phoenix.schema.PTable)10 PUnsignedLong (org.apache.phoenix.schema.types.PUnsignedLong)9 IOException (java.io.IOException)8 BlockingRpcCallback (org.apache.hadoop.hbase.ipc.BlockingRpcCallback)8 ServerRpcController (org.apache.hadoop.hbase.ipc.ServerRpcController)8 MutationProto (org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto)8 MetaDataResponse (org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataResponse)8 MetaDataService (org.apache.phoenix.coprocessor.generated.MetaDataProtos.MetaDataService)8 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)8 PhoenixIndexBuilder (org.apache.phoenix.index.PhoenixIndexBuilder)8 TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)8 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)7 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 Batch (org.apache.hadoop.hbase.client.coprocessor.Batch)7