Search in sources :

Example 11 with ThreadContextClassLoader

use of com.facebook.presto.spi.classloader.ThreadContextClassLoader in project presto by prestodb.

the class PluginManager method loadPlugin.

private void loadPlugin(String plugin) throws Exception {
    log.info("-- Loading plugin %s --", plugin);
    URLClassLoader pluginClassLoader = buildClassLoader(plugin);
    try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(pluginClassLoader)) {
        loadPlugin(pluginClassLoader);
    }
    log.info("-- Finished loading plugin %s --", plugin);
}
Also used : URLClassLoader(java.net.URLClassLoader) ThreadContextClassLoader(com.facebook.presto.spi.classloader.ThreadContextClassLoader)

Example 12 with ThreadContextClassLoader

use of com.facebook.presto.spi.classloader.ThreadContextClassLoader in project presto by prestodb.

the class TestShardWriter method testWriter.

@Test
public void testWriter() throws Exception {
    List<Long> columnIds = ImmutableList.of(1L, 2L, 4L, 6L, 7L, 8L, 9L, 10L);
    ArrayType arrayType = new ArrayType(BIGINT);
    ArrayType arrayOfArrayType = new ArrayType(arrayType);
    MapType mapType = new MapType(createVarcharType(10), BOOLEAN);
    List<Type> columnTypes = ImmutableList.of(BIGINT, createVarcharType(10), VARBINARY, DOUBLE, BOOLEAN, arrayType, mapType, arrayOfArrayType);
    File file = new File(directory, System.nanoTime() + ".orc");
    byte[] bytes1 = octets(0x00, 0xFE, 0xFF);
    byte[] bytes3 = octets(0x01, 0x02, 0x19, 0x80);
    RowPagesBuilder rowPagesBuilder = RowPagesBuilder.rowPagesBuilder(columnTypes).row(123L, "hello", wrappedBuffer(bytes1), 123.456, true, arrayBlockOf(BIGINT, 1, 2), mapBlockOf(createVarcharType(5), BOOLEAN, "k1", true), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 5))).row(null, "world", null, Double.POSITIVE_INFINITY, null, arrayBlockOf(BIGINT, 3, null), mapBlockOf(createVarcharType(5), BOOLEAN, "k2", null), arrayBlockOf(arrayType, null, arrayBlockOf(BIGINT, 6, 7))).row(456L, "bye ☃", wrappedBuffer(bytes3), Double.NaN, false, arrayBlockOf(BIGINT), mapBlockOf(createVarcharType(5), BOOLEAN, "k3", false), arrayBlockOf(arrayType, arrayBlockOf(BIGINT)));
    try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(new EmptyClassLoader());
        OrcFileWriter writer = new OrcFileWriter(columnIds, columnTypes, file)) {
        writer.appendPages(rowPagesBuilder.build());
    }
    try (OrcDataSource dataSource = fileOrcDataSource(file)) {
        OrcRecordReader reader = createReader(dataSource, columnIds, columnTypes);
        assertEquals(reader.getReaderRowCount(), 3);
        assertEquals(reader.getReaderPosition(), 0);
        assertEquals(reader.getFileRowCount(), reader.getReaderRowCount());
        assertEquals(reader.getFilePosition(), reader.getFilePosition());
        assertEquals(reader.nextBatch(), 3);
        assertEquals(reader.getReaderPosition(), 0);
        assertEquals(reader.getFilePosition(), reader.getFilePosition());
        Block column0 = reader.readBlock(BIGINT, 0);
        assertEquals(column0.isNull(0), false);
        assertEquals(column0.isNull(1), true);
        assertEquals(column0.isNull(2), false);
        assertEquals(BIGINT.getLong(column0, 0), 123L);
        assertEquals(BIGINT.getLong(column0, 2), 456L);
        Block column1 = reader.readBlock(createVarcharType(10), 1);
        assertEquals(createVarcharType(10).getSlice(column1, 0), utf8Slice("hello"));
        assertEquals(createVarcharType(10).getSlice(column1, 1), utf8Slice("world"));
        assertEquals(createVarcharType(10).getSlice(column1, 2), utf8Slice("bye ☃"));
        Block column2 = reader.readBlock(VARBINARY, 2);
        assertEquals(VARBINARY.getSlice(column2, 0), wrappedBuffer(bytes1));
        assertEquals(column2.isNull(1), true);
        assertEquals(VARBINARY.getSlice(column2, 2), wrappedBuffer(bytes3));
        Block column3 = reader.readBlock(DOUBLE, 3);
        assertEquals(column3.isNull(0), false);
        assertEquals(column3.isNull(1), false);
        assertEquals(column3.isNull(2), false);
        assertEquals(DOUBLE.getDouble(column3, 0), 123.456);
        assertEquals(DOUBLE.getDouble(column3, 1), Double.POSITIVE_INFINITY);
        assertEquals(DOUBLE.getDouble(column3, 2), Double.NaN);
        Block column4 = reader.readBlock(BOOLEAN, 4);
        assertEquals(column4.isNull(0), false);
        assertEquals(column4.isNull(1), true);
        assertEquals(column4.isNull(2), false);
        assertEquals(BOOLEAN.getBoolean(column4, 0), true);
        assertEquals(BOOLEAN.getBoolean(column4, 2), false);
        Block column5 = reader.readBlock(arrayType, 5);
        assertEquals(column5.getPositionCount(), 3);
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column5, 0), arrayBlockOf(BIGINT, 1, 2)));
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column5, 1), arrayBlockOf(BIGINT, 3, null)));
        assertTrue(arrayBlocksEqual(BIGINT, arrayType.getObject(column5, 2), arrayBlockOf(BIGINT)));
        Block column6 = reader.readBlock(mapType, 6);
        assertEquals(column6.getPositionCount(), 3);
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column6, 0), mapBlockOf(createVarcharType(5), BOOLEAN, "k1", true)));
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column6, 1), mapBlockOf(createVarcharType(5), BOOLEAN, "k2", null)));
        assertTrue(mapBlocksEqual(createVarcharType(5), BOOLEAN, arrayType.getObject(column6, 2), mapBlockOf(createVarcharType(5), BOOLEAN, "k3", false)));
        Block column7 = reader.readBlock(arrayOfArrayType, 7);
        assertEquals(column7.getPositionCount(), 3);
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column7, 0), arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 5))));
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column7, 1), arrayBlockOf(arrayType, null, arrayBlockOf(BIGINT, 6, 7))));
        assertTrue(arrayBlocksEqual(arrayType, arrayOfArrayType.getObject(column7, 2), arrayBlockOf(arrayType, arrayBlockOf(BIGINT))));
        assertEquals(reader.nextBatch(), -1);
        assertEquals(reader.getReaderPosition(), 3);
        assertEquals(reader.getFilePosition(), reader.getFilePosition());
        OrcFileMetadata orcFileMetadata = METADATA_CODEC.fromJson(reader.getUserMetadata().get(OrcFileMetadata.KEY).getBytes());
        assertEquals(orcFileMetadata, new OrcFileMetadata(ImmutableMap.<Long, TypeSignature>builder().put(1L, BIGINT.getTypeSignature()).put(2L, createVarcharType(10).getTypeSignature()).put(4L, VARBINARY.getTypeSignature()).put(6L, DOUBLE.getTypeSignature()).put(7L, BOOLEAN.getTypeSignature()).put(8L, arrayType.getTypeSignature()).put(9L, mapType.getTypeSignature()).put(10L, arrayOfArrayType.getTypeSignature()).build()));
    }
    File crcFile = new File(file.getParentFile(), "." + file.getName() + ".crc");
    assertFalse(crcFile.exists());
}
Also used : OrcDataSource(com.facebook.presto.orc.OrcDataSource) OrcTestingUtil.fileOrcDataSource(com.facebook.presto.raptor.storage.OrcTestingUtil.fileOrcDataSource) RowPagesBuilder(com.facebook.presto.RowPagesBuilder) OrcRecordReader(com.facebook.presto.orc.OrcRecordReader) MapType(com.facebook.presto.type.MapType) ArrayType(com.facebook.presto.type.ArrayType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) VarcharType.createVarcharType(com.facebook.presto.spi.type.VarcharType.createVarcharType) Block(com.facebook.presto.spi.block.Block) File(java.io.File) ThreadContextClassLoader(com.facebook.presto.spi.classloader.ThreadContextClassLoader) Test(org.testng.annotations.Test)

Example 13 with ThreadContextClassLoader

use of com.facebook.presto.spi.classloader.ThreadContextClassLoader in project presto by prestodb.

the class OrcFileRewriter method rewrite.

public static OrcFileInfo rewrite(File input, File output, BitSet rowsToDelete) throws IOException {
    try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(FileSystem.class.getClassLoader());
        FileSystem fileSystem = new SyncingFileSystem(CONFIGURATION)) {
        Reader reader = createReader(fileSystem, path(input));
        if (reader.getNumberOfRows() < rowsToDelete.length()) {
            throw new IOException("File has fewer rows than deletion vector");
        }
        int deleteRowCount = rowsToDelete.cardinality();
        if (reader.getNumberOfRows() == deleteRowCount) {
            return new OrcFileInfo(0, 0);
        }
        if (reader.getNumberOfRows() >= Integer.MAX_VALUE) {
            throw new IOException("File has too many rows");
        }
        int inputRowCount = toIntExact(reader.getNumberOfRows());
        WriterOptions writerOptions = new OrcWriterOptions(CONFIGURATION).memory(new NullMemoryManager(CONFIGURATION)).fileSystem(fileSystem).compress(reader.getCompression()).inspector(reader.getObjectInspector());
        long start = System.nanoTime();
        try (Closer<RecordReader, IOException> recordReader = closer(reader.rows(), RecordReader::close);
            Closer<Writer, IOException> writer = closer(createWriter(path(output), writerOptions), Writer::close)) {
            if (reader.hasMetadataValue(OrcFileMetadata.KEY)) {
                ByteBuffer orcFileMetadata = reader.getMetadataValue(OrcFileMetadata.KEY);
                writer.get().addUserMetadata(OrcFileMetadata.KEY, orcFileMetadata);
            }
            OrcFileInfo fileInfo = rewrite(recordReader.get(), writer.get(), rowsToDelete, inputRowCount);
            log.debug("Rewrote file %s in %s (input rows: %s, output rows: %s)", input.getName(), nanosSince(start), inputRowCount, inputRowCount - deleteRowCount);
            return fileInfo;
        }
    }
}
Also used : RecordReader(org.apache.hadoop.hive.ql.io.orc.RecordReader) RecordReader(org.apache.hadoop.hive.ql.io.orc.RecordReader) Reader(org.apache.hadoop.hive.ql.io.orc.Reader) OrcFile.createReader(org.apache.hadoop.hive.ql.io.orc.OrcFile.createReader) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) NullMemoryManager(org.apache.hadoop.hive.ql.io.orc.NullMemoryManager) ByteBuffer(java.nio.ByteBuffer) OrcWriterOptions(org.apache.hadoop.hive.ql.io.orc.OrcWriterOptions) SyncingFileSystem(com.facebook.presto.raptor.util.SyncingFileSystem) SyncingFileSystem(com.facebook.presto.raptor.util.SyncingFileSystem) FileSystem(org.apache.hadoop.fs.FileSystem) OrcWriterOptions(org.apache.hadoop.hive.ql.io.orc.OrcWriterOptions) WriterOptions(org.apache.hadoop.hive.ql.io.orc.OrcFile.WriterOptions) ThreadContextClassLoader(com.facebook.presto.spi.classloader.ThreadContextClassLoader) Writer(org.apache.hadoop.hive.ql.io.orc.Writer) OrcFile.createWriter(org.apache.hadoop.hive.ql.io.orc.OrcFile.createWriter)

Example 14 with ThreadContextClassLoader

use of com.facebook.presto.spi.classloader.ThreadContextClassLoader in project presto by prestodb.

the class HiveConnector method commit.

@Override
public void commit(ConnectorTransactionHandle transaction) {
    HiveMetadata metadata = (HiveMetadata) transactionManager.remove(transaction);
    checkArgument(metadata != null, "no such transaction: %s", transaction);
    try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(classLoader)) {
        metadata.commit();
    }
}
Also used : ThreadContextClassLoader(com.facebook.presto.spi.classloader.ThreadContextClassLoader)

Example 15 with ThreadContextClassLoader

use of com.facebook.presto.spi.classloader.ThreadContextClassLoader in project presto by prestodb.

the class ConnectorManager method removeConnectorInternal.

private synchronized void removeConnectorInternal(ConnectorId connectorId) {
    splitManager.removeConnectorSplitManager(connectorId);
    pageSourceManager.removeConnectorPageSourceProvider(connectorId);
    pageSinkManager.removeConnectorPageSinkProvider(connectorId);
    indexManager.removeIndexProvider(connectorId);
    nodePartitioningManager.removePartitioningProvider(connectorId);
    metadataManager.getProcedureRegistry().removeProcedures(connectorId);
    accessControlManager.removeCatalogAccessControl(connectorId);
    metadataManager.getTablePropertyManager().removeProperties(connectorId);
    metadataManager.getSchemaPropertyManager().removeProperties(connectorId);
    metadataManager.getSessionPropertyManager().removeConnectorSessionProperties(connectorId);
    Connector connector = connectors.remove(connectorId).getConnector();
    if (connector != null) {
        try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(connector.getClass().getClassLoader())) {
            connector.shutdown();
        } catch (Throwable t) {
            log.error(t, "Error shutting down connector: %s", connectorId);
        }
    }
}
Also used : SystemConnector(com.facebook.presto.connector.system.SystemConnector) InformationSchemaConnector(com.facebook.presto.connector.informationSchema.InformationSchemaConnector) Connector(com.facebook.presto.spi.connector.Connector) ThreadContextClassLoader(com.facebook.presto.spi.classloader.ThreadContextClassLoader)

Aggregations

ThreadContextClassLoader (com.facebook.presto.spi.classloader.ThreadContextClassLoader)15 Injector (com.google.inject.Injector)7 Bootstrap (io.airlift.bootstrap.Bootstrap)7 JsonModule (io.airlift.json.JsonModule)6 Connector (com.facebook.presto.spi.connector.Connector)2 ClassLoaderSafeConnectorPageSourceProvider (com.facebook.presto.spi.connector.classloader.ClassLoaderSafeConnectorPageSourceProvider)2 ClassLoaderSafeConnectorSplitManager (com.facebook.presto.spi.connector.classloader.ClassLoaderSafeConnectorSplitManager)2 LifeCycleManager (io.airlift.bootstrap.LifeCycleManager)2 IOException (java.io.IOException)2 RowPagesBuilder (com.facebook.presto.RowPagesBuilder)1 SECURITY_FILE (com.facebook.presto.atop.AtopConnectorConfig.SECURITY_FILE)1 SECURITY_NONE (com.facebook.presto.atop.AtopConnectorConfig.SECURITY_NONE)1 InformationSchemaConnector (com.facebook.presto.connector.informationSchema.InformationSchemaConnector)1 SystemConnector (com.facebook.presto.connector.system.SystemConnector)1 HiveAuthenticationModule (com.facebook.presto.hive.authentication.HiveAuthenticationModule)1 HiveMetastoreModule (com.facebook.presto.hive.metastore.HiveMetastoreModule)1 HiveSecurityModule (com.facebook.presto.hive.security.HiveSecurityModule)1 OrcDataSource (com.facebook.presto.orc.OrcDataSource)1 OrcRecordReader (com.facebook.presto.orc.OrcRecordReader)1 AllowAllAccessControlModule (com.facebook.presto.plugin.base.security.AllowAllAccessControlModule)1