Search in sources :

Example 1 with PMetaData

use of org.apache.phoenix.schema.PMetaData in project phoenix by apache.

the class ConnectionQueryServicesImpl method metaDataMutated.

/**
     * Ensures that metaData mutations are handled in the correct order
     */
private PMetaData metaDataMutated(PName tenantId, String tableName, long tableSeqNum, Mutator mutator) throws SQLException {
    synchronized (latestMetaDataLock) {
        throwConnectionClosedIfNullMetaData();
        PMetaData metaData = latestMetaData;
        PTable table;
        long endTime = System.currentTimeMillis() + DEFAULT_OUT_OF_ORDER_MUTATIONS_WAIT_TIME_MS;
        while (true) {
            try {
                try {
                    table = metaData.getTableRef(new PTableKey(tenantId, tableName)).getTable();
                    /* If the table is at the prior sequence number, then we're good to go.
                         * We know if we've got this far, that the server validated the mutations,
                         * so we'd just need to wait until the other connection that mutated the same
                         * table is processed.
                         */
                    if (table.getSequenceNumber() + 1 == tableSeqNum) {
                        // TODO: assert that timeStamp is bigger that table timeStamp?
                        mutator.mutate(metaData);
                        break;
                    } else if (table.getSequenceNumber() >= tableSeqNum) {
                        logger.warn("Attempt to cache older version of " + tableName + ": current= " + table.getSequenceNumber() + ", new=" + tableSeqNum);
                        break;
                    }
                } catch (TableNotFoundException e) {
                }
                long waitTime = endTime - System.currentTimeMillis();
                // and the next time it's used it'll be pulled over from the server.
                if (waitTime <= 0) {
                    logger.warn("Unable to update meta data repo within " + (DEFAULT_OUT_OF_ORDER_MUTATIONS_WAIT_TIME_MS / 1000) + " seconds for " + tableName);
                    // There will never be a parentTableName here, as that would only
                    // be non null for an index an we never add/remove columns from an index.
                    metaData.removeTable(tenantId, tableName, null, HConstants.LATEST_TIMESTAMP);
                    break;
                }
                latestMetaDataLock.wait(waitTime);
            } catch (InterruptedException e) {
                // restore the interrupt status
                Thread.currentThread().interrupt();
                throw new SQLExceptionInfo.Builder(SQLExceptionCode.INTERRUPTED_EXCEPTION).setRootCause(e).build().buildException();
            }
        }
        latestMetaData = metaData;
        latestMetaDataLock.notifyAll();
        return metaData;
    }
}
Also used : TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PMetaData(org.apache.phoenix.schema.PMetaData) 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) PTableKey(org.apache.phoenix.schema.PTableKey) PTable(org.apache.phoenix.schema.PTable)

Example 2 with PMetaData

use of org.apache.phoenix.schema.PMetaData 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 3 with PMetaData

use of org.apache.phoenix.schema.PMetaData in project phoenix by apache.

the class ConnectionQueryServicesImpl method connect.

@Override
public PhoenixConnection connect(String url, Properties info) throws SQLException {
    checkClosed();
    PMetaData metadata = latestMetaData;
    throwConnectionClosedIfNullMetaData();
    metadata = metadata.clone();
    return new PhoenixConnection(this, url, info, metadata);
}
Also used : PMetaData(org.apache.phoenix.schema.PMetaData) PhoenixConnection(org.apache.phoenix.jdbc.PhoenixConnection)

Example 4 with PMetaData

use of org.apache.phoenix.schema.PMetaData in project phoenix by apache.

the class MutationState method shouldResubmitTransaction.

/**
     * Determines whether indexes were added to mutated tables while the transaction was in progress.
     * @return true if indexes were added and false otherwise.
     * @throws SQLException 
     */
private boolean shouldResubmitTransaction(Set<TableRef> txTableRefs) throws SQLException {
    if (logger.isInfoEnabled())
        logger.info("Checking for index updates as of " + getInitialWritePointer());
    MetaDataClient client = new MetaDataClient(connection);
    PMetaData cache = connection.getMetaDataCache();
    boolean addedAnyIndexes = false;
    boolean allImmutableTables = !txTableRefs.isEmpty();
    for (TableRef tableRef : txTableRefs) {
        PTable dataTable = tableRef.getTable();
        List<PTable> oldIndexes;
        PTableRef ptableRef = cache.getTableRef(dataTable.getKey());
        oldIndexes = ptableRef.getTable().getIndexes();
        // Always check at server for metadata change, as it's possible that the table is configured to not check for metadata changes
        // but in this case, the tx manager is telling us it's likely that there has been a change.
        MetaDataMutationResult result = client.updateCache(dataTable.getTenantId(), dataTable.getSchemaName().getString(), dataTable.getTableName().getString(), true);
        long timestamp = TransactionUtil.getResolvedTime(connection, result);
        tableRef.setTimeStamp(timestamp);
        PTable updatedDataTable = result.getTable();
        if (updatedDataTable == null) {
            throw new TableNotFoundException(dataTable.getSchemaName().getString(), dataTable.getTableName().getString());
        }
        allImmutableTables &= updatedDataTable.isImmutableRows();
        tableRef.setTable(updatedDataTable);
        if (!addedAnyIndexes) {
            // TODO: in theory we should do a deep equals check here, as it's possible
            // that an index was dropped and recreated with the same name but different
            // indexed/covered columns.
            addedAnyIndexes = (!oldIndexes.equals(updatedDataTable.getIndexes()));
            if (logger.isInfoEnabled())
                logger.info((addedAnyIndexes ? "Updates " : "No updates ") + "as of " + timestamp + " to " + updatedDataTable.getName().getString() + " with indexes " + updatedDataTable.getIndexes());
        }
    }
    if (logger.isInfoEnabled())
        logger.info((addedAnyIndexes ? "Updates " : "No updates ") + "to indexes as of " + getInitialWritePointer() + " over " + (allImmutableTables ? " all immutable tables" : " some mutable tables"));
    // If any indexes were added, then the conflict might be due to DDL/DML fence.
    return allImmutableTables || addedAnyIndexes;
}
Also used : MetaDataClient(org.apache.phoenix.schema.MetaDataClient) TableNotFoundException(org.apache.phoenix.schema.TableNotFoundException) PMetaData(org.apache.phoenix.schema.PMetaData) PTableRef(org.apache.phoenix.schema.PTableRef) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult) PTableRef(org.apache.phoenix.schema.PTableRef) TableRef(org.apache.phoenix.schema.TableRef) PTable(org.apache.phoenix.schema.PTable)

Aggregations

PMetaData (org.apache.phoenix.schema.PMetaData)4 PTable (org.apache.phoenix.schema.PTable)3 TableNotFoundException (org.apache.phoenix.schema.TableNotFoundException)3 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)2 PTableKey (org.apache.phoenix.schema.PTableKey)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1 NonTxIndexBuilder (org.apache.phoenix.hbase.index.covered.NonTxIndexBuilder)1 KeyValueBuilder (org.apache.phoenix.hbase.index.util.KeyValueBuilder)1 PhoenixIndexBuilder (org.apache.phoenix.index.PhoenixIndexBuilder)1 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)1 MetaDataClient (org.apache.phoenix.schema.MetaDataClient)1 PTableRef (org.apache.phoenix.schema.PTableRef)1 TableRef (org.apache.phoenix.schema.TableRef)1