Search in sources :

Example 31 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class MetaDataEndpointImpl method findAllChildViews.

private void findAllChildViews(Region region, byte[] tenantId, PTable table, TableViewFinder result, long clientTimeStamp) throws IOException, SQLException {
    TableViewFinder currResult = findChildViews(region, tenantId, table);
    result.addResult(currResult);
    for (ViewInfo viewInfo : currResult.getViewInfoList()) {
        byte[] viewtenantId = viewInfo.getTenantId();
        byte[] viewSchema = viewInfo.getSchemaName();
        byte[] viewTable = viewInfo.getViewName();
        byte[] tableKey = SchemaUtil.getTableKey(viewtenantId, viewSchema, viewTable);
        ImmutableBytesPtr cacheKey = new ImmutableBytesPtr(tableKey);
        PTable view = loadTable(env, tableKey, cacheKey, clientTimeStamp, clientTimeStamp);
        findAllChildViews(region, viewtenantId, view, result, clientTimeStamp);
    }
}
Also used : ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) PTable(org.apache.phoenix.schema.PTable)

Example 32 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class MetaDataEndpointImpl method updateViewHeaderRow.

/**
     * Updates the base table column count, column count, sequence number and ordinal postions of
     * columns of the view based on the columns being added or dropped.
     */
private void updateViewHeaderRow(PTable basePhysicalTable, List<Mutation> tableMetadata, List<Mutation> mutationsForAddingColumnsToViews, List<ImmutableBytesPtr> invalidateList, long clientTimeStamp, short viewColumnDelta, short baseTableColumnDelta, byte[] viewKey, PTable view, ColumnOrdinalPositionUpdateList ordinalPositionList, int numCols, boolean changeSequenceNumber) {
    // Update the view header rows with new column counts.
    Put viewHeaderRowPut = new Put(viewKey, clientTimeStamp);
    if (!isDivergedView(view) && baseTableColumnDelta != 0) {
        // Base column count should only be updated for diverged views.
        int oldBaseColumnCount = view.getBaseColumnCount();
        byte[] baseColumnCountPtr = new byte[PInteger.INSTANCE.getByteSize()];
        PInteger.INSTANCE.getCodec().encodeInt(oldBaseColumnCount + baseTableColumnDelta, baseColumnCountPtr, 0);
        viewHeaderRowPut.add(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.BASE_COLUMN_COUNT_BYTES, clientTimeStamp, baseColumnCountPtr);
    }
    if (viewColumnDelta != 0) {
        byte[] columnCountPtr = new byte[PInteger.INSTANCE.getByteSize()];
        PInteger.INSTANCE.getCodec().encodeInt(numCols + viewColumnDelta, columnCountPtr, 0);
        viewHeaderRowPut.add(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.COLUMN_COUNT_BYTES, clientTimeStamp, columnCountPtr);
    }
    if (changeSequenceNumber) {
        byte[] viewSequencePtr = new byte[PLong.INSTANCE.getByteSize()];
        PLong.INSTANCE.getCodec().encodeLong(view.getSequenceNumber() + 1, viewSequencePtr, 0);
        viewHeaderRowPut.add(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.TABLE_SEQ_NUM_BYTES, clientTimeStamp, viewSequencePtr);
        mutationsForAddingColumnsToViews.add(viewHeaderRowPut);
        // only invalidate if the sequence number is about to change
        invalidateList.add(new ImmutableBytesPtr(viewKey));
        // Update the ordinal positions. The list would be non-empty only if the sequence
        // number will change.
        int i = 0;
        for (byte[] columnKey : ordinalPositionList.columnKeys) {
            int ordinalPosition = ordinalPositionList.getOrdinalPositionFromListIdx(i);
            Put positionUpdatePut = new Put(columnKey, clientTimeStamp);
            byte[] ptr = new byte[PInteger.INSTANCE.getByteSize()];
            PInteger.INSTANCE.getCodec().encodeInt(ordinalPosition, ptr, 0);
            positionUpdatePut.add(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, PhoenixDatabaseMetaData.ORDINAL_POSITION_BYTES, clientTimeStamp, ptr);
            mutationsForAddingColumnsToViews.add(positionUpdatePut);
            i++;
        }
    }
    if (view.rowKeyOrderOptimizable()) {
        UpgradeUtil.addRowKeyOrderOptimizableCell(mutationsForAddingColumnsToViews, viewKey, clientTimeStamp);
    }
    // if switching from from non tx to tx
    if (!basePhysicalTable.isTransactional() && switchAttribute(basePhysicalTable, basePhysicalTable.isTransactional(), tableMetadata, TRANSACTIONAL_BYTES)) {
        invalidateList.add(new ImmutableBytesPtr(viewKey));
        Put put = new Put(viewKey);
        put.add(PhoenixDatabaseMetaData.TABLE_FAMILY_BYTES, TRANSACTIONAL_BYTES, clientTimeStamp, PBoolean.INSTANCE.toBytes(true));
        mutationsForAddingColumnsToViews.add(put);
    }
}
Also used : ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) Put(org.apache.hadoop.hbase.client.Put) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint)

Example 33 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr 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 {
        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 {
        region.releaseRowLocks(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 34 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class MetaDataEndpointImpl method mutateColumn.

private MetaDataMutationResult mutateColumn(List<Mutation> tableMetadata, ColumnMutator mutator) throws IOException {
    byte[][] rowKeyMetaData = new byte[5][];
    MetaDataUtil.getTenantIdAndSchemaAndTableName(tableMetadata, rowKeyMetaData);
    byte[] tenantId = rowKeyMetaData[PhoenixDatabaseMetaData.TENANT_ID_INDEX];
    byte[] schemaName = rowKeyMetaData[PhoenixDatabaseMetaData.SCHEMA_NAME_INDEX];
    byte[] tableName = rowKeyMetaData[PhoenixDatabaseMetaData.TABLE_NAME_INDEX];
    byte[] key = SchemaUtil.getTableKey(tenantId, schemaName, tableName);
    try {
        Region region = env.getRegion();
        MetaDataMutationResult result = checkTableKeyInRegion(key, region);
        if (result != null) {
            return result;
        }
        List<RowLock> locks = Lists.newArrayList();
        try {
            acquireLock(region, key, locks);
            ImmutableBytesPtr cacheKey = new ImmutableBytesPtr(key);
            List<ImmutableBytesPtr> invalidateList = new ArrayList<ImmutableBytesPtr>();
            invalidateList.add(cacheKey);
            Cache<ImmutableBytesPtr, PMetaDataEntity> metaDataCache = GlobalCache.getInstance(this.env).getMetaDataCache();
            PTable table = (PTable) metaDataCache.getIfPresent(cacheKey);
            if (logger.isDebugEnabled()) {
                if (table == null) {
                    logger.debug("Table " + Bytes.toStringBinary(key) + " not found in cache. Will build through scan");
                } else {
                    logger.debug("Table " + Bytes.toStringBinary(key) + " found in cache with timestamp " + table.getTimeStamp() + " seqNum " + table.getSequenceNumber());
                }
            }
            // Get client timeStamp from mutations
            long clientTimeStamp = MetaDataUtil.getClientTimeStamp(tableMetadata);
            if (table == null && (table = buildTable(key, cacheKey, region, HConstants.LATEST_TIMESTAMP)) == null) {
                // if not found then call newerTableExists and add delete marker for timestamp
                // found
                table = buildDeletedTable(key, cacheKey, region, clientTimeStamp);
                if (table != null) {
                    logger.info("Found newer table deleted as of " + table.getTimeStamp() + " versus client timestamp of " + clientTimeStamp);
                    return new MetaDataMutationResult(MutationCode.NEWER_TABLE_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
                }
                return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
            }
            if (table.getTimeStamp() >= clientTimeStamp) {
                logger.info("Found newer table as of " + table.getTimeStamp() + " versus client timestamp of " + clientTimeStamp);
                return new MetaDataMutationResult(MutationCode.NEWER_TABLE_FOUND, EnvironmentEdgeManager.currentTimeMillis(), table);
            } else if (isTableDeleted(table)) {
                return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
            }
            // lookup
            long expectedSeqNum = MetaDataUtil.getSequenceNumber(tableMetadata) - 1;
            // tableMetaData
            if (logger.isDebugEnabled()) {
                logger.debug("For table " + Bytes.toStringBinary(key) + " expecting seqNum " + expectedSeqNum + " and found seqNum " + table.getSequenceNumber() + " with " + table.getColumns().size() + " columns: " + table.getColumns());
            }
            if (expectedSeqNum != table.getSequenceNumber()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("For table " + Bytes.toStringBinary(key) + " returning CONCURRENT_TABLE_MUTATION due to unexpected seqNum");
                }
                return new MetaDataMutationResult(MutationCode.CONCURRENT_TABLE_MUTATION, EnvironmentEdgeManager.currentTimeMillis(), table);
            }
            PTableType type = table.getType();
            if (type == PTableType.INDEX) {
                // Disallow mutation of an index table
                return new MetaDataMutationResult(MutationCode.UNALLOWED_TABLE_MUTATION, EnvironmentEdgeManager.currentTimeMillis(), null);
            } else {
                // server-side, except for indexing, we always expect the keyvalues to be standard KeyValues
                PTableType expectedType = MetaDataUtil.getTableType(tableMetadata, GenericKeyValueBuilder.INSTANCE, new ImmutableBytesWritable());
                // We said to drop a table, but found a view or visa versa
                if (type != expectedType) {
                    return new MetaDataMutationResult(MutationCode.TABLE_NOT_FOUND, EnvironmentEdgeManager.currentTimeMillis(), null);
                }
            }
            result = mutator.updateMutation(table, rowKeyMetaData, tableMetadata, region, invalidateList, locks, clientTimeStamp);
            // if the update mutation caused tables to be deleted, the mutation code returned will be MutationCode.TABLE_ALREADY_EXISTS 
            if (result != null && result.getMutationCode() != MutationCode.TABLE_ALREADY_EXISTS) {
                return result;
            }
            region.mutateRowsWithLocks(tableMetadata, Collections.<byte[]>emptySet(), HConstants.NO_NONCE, HConstants.NO_NONCE);
            // Invalidate from cache
            for (ImmutableBytesPtr invalidateKey : invalidateList) {
                metaDataCache.invalidate(invalidateKey);
            }
            // Get client timeStamp from mutations, since it may get updated by the
            // mutateRowsWithLocks call
            long currentTime = MetaDataUtil.getClientTimeStamp(tableMetadata);
            // if the update mutation caused tables to be deleted just return the result which will contain the table to be deleted
            if (result != null) {
                return result;
            } else {
                table = buildTable(key, cacheKey, region, HConstants.LATEST_TIMESTAMP);
                return new MetaDataMutationResult(MutationCode.TABLE_ALREADY_EXISTS, currentTime, table);
            }
        } finally {
            region.releaseRowLocks(locks);
        }
    } catch (Throwable t) {
        ServerUtil.throwIOException(SchemaUtil.getTableName(schemaName, tableName), t);
        // impossible
        return null;
    }
}
Also used : ImmutableBytesWritable(org.apache.hadoop.hbase.io.ImmutableBytesWritable) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) PTableType(org.apache.phoenix.schema.PTableType) ArrayList(java.util.ArrayList) PTable(org.apache.phoenix.schema.PTable) PMetaDataEntity(org.apache.phoenix.schema.PMetaDataEntity) Region(org.apache.hadoop.hbase.regionserver.Region) RowLock(org.apache.hadoop.hbase.regionserver.Region.RowLock)

Example 35 with ImmutableBytesPtr

use of org.apache.phoenix.hbase.index.util.ImmutableBytesPtr in project phoenix by apache.

the class MetaDataEndpointImpl method clearCache.

@Override
public void clearCache(RpcController controller, ClearCacheRequest request, RpcCallback<ClearCacheResponse> done) {
    GlobalCache cache = GlobalCache.getInstance(this.env);
    Cache<ImmutableBytesPtr, PMetaDataEntity> metaDataCache = GlobalCache.getInstance(this.env).getMetaDataCache();
    metaDataCache.invalidateAll();
    long unfreedBytes = cache.clearTenantCache();
    ClearCacheResponse.Builder builder = ClearCacheResponse.newBuilder();
    builder.setUnfreedBytes(unfreedBytes);
    done.run(builder.build());
}
Also used : GlobalCache(org.apache.phoenix.cache.GlobalCache) PMetaDataEntity(org.apache.phoenix.schema.PMetaDataEntity) ImmutableBytesPtr(org.apache.phoenix.hbase.index.util.ImmutableBytesPtr) ClearCacheResponse(org.apache.phoenix.coprocessor.generated.MetaDataProtos.ClearCacheResponse)

Aggregations

ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)120 Mutation (org.apache.hadoop.hbase.client.Mutation)31 PTable (org.apache.phoenix.schema.PTable)28 ArrayList (java.util.ArrayList)27 Region (org.apache.hadoop.hbase.regionserver.Region)22 PMetaDataEntity (org.apache.phoenix.schema.PMetaDataEntity)22 Test (org.junit.Test)21 Cell (org.apache.hadoop.hbase.Cell)20 Put (org.apache.hadoop.hbase.client.Put)18 List (java.util.List)15 Scan (org.apache.hadoop.hbase.client.Scan)15 Pair (org.apache.hadoop.hbase.util.Pair)15 IOException (java.io.IOException)14 Expression (org.apache.phoenix.expression.Expression)14 PColumn (org.apache.phoenix.schema.PColumn)14 RowLock (org.apache.hadoop.hbase.regionserver.Region.RowLock)13 PSmallint (org.apache.phoenix.schema.types.PSmallint)12 HashMap (java.util.HashMap)11 ImmutableBytesWritable (org.apache.hadoop.hbase.io.ImmutableBytesWritable)11 LiteralExpression (org.apache.phoenix.expression.LiteralExpression)11