Search in sources :

Example 1 with LedgerOffloaderFactory

use of org.apache.bookkeeper.mledger.LedgerOffloaderFactory in project pulsar by apache.

the class OffloaderUtils method getOffloaderFactory.

/**
 * Extract the Pulsar offloader class from a offloader archive.
 *
 * @param narPath nar package path
 * @return the offloader class name
 * @throws IOException when fail to retrieve the pulsar offloader class
 */
static Pair<NarClassLoader, LedgerOffloaderFactory> getOffloaderFactory(String narPath, String narExtractionDirectory) throws IOException {
    // need to load offloader NAR to the classloader that also loaded LedgerOffloaderFactory in case
    // LedgerOffloaderFactory is loaded by a classloader that is not the default classloader
    // as is the case for the pulsar presto plugin
    NarClassLoader ncl = NarClassLoaderBuilder.builder().narFile(new File(narPath)).parentClassLoader(LedgerOffloaderFactory.class.getClassLoader()).extractionDirectory(narExtractionDirectory).build();
    String configStr = ncl.getServiceDefinition(PULSAR_OFFLOADER_SERVICE_NAME);
    OffloaderDefinition conf = ObjectMapperFactory.getThreadLocalYaml().readValue(configStr, OffloaderDefinition.class);
    if (StringUtils.isEmpty(conf.getOffloaderFactoryClass())) {
        throw new IOException(String.format("The '%s' offloader does not provide an offloader factory implementation", conf.getName()));
    }
    try {
        // Try to load offloader factory class and check it implements Offloader interface
        Class factoryClass = ncl.loadClass(conf.getOffloaderFactoryClass());
        CompletableFuture<LedgerOffloaderFactory> loadFuture = new CompletableFuture<>();
        Thread loadingThread = new Thread(() -> {
            Thread.currentThread().setContextClassLoader(ncl);
            try {
                Object offloader = factoryClass.getDeclaredConstructor().newInstance();
                if (!(offloader instanceof LedgerOffloaderFactory)) {
                    throw new IOException("Class " + conf.getOffloaderFactoryClass() + " does not implement " + "interface " + LedgerOffloaderFactory.class.getName());
                }
                loadFuture.complete((LedgerOffloaderFactory) offloader);
            } catch (Throwable t) {
                loadFuture.completeExceptionally(t);
            }
        }, "load-factory-" + factoryClass);
        try {
            loadingThread.start();
            return Pair.of(ncl, loadFuture.get());
        } finally {
            loadingThread.join();
        }
    } catch (Throwable t) {
        rethrowIOException(t);
    }
    return null;
}
Also used : LedgerOffloaderFactory(org.apache.bookkeeper.mledger.LedgerOffloaderFactory) CompletableFuture(java.util.concurrent.CompletableFuture) NarClassLoader(org.apache.pulsar.common.nar.NarClassLoader) IOException(java.io.IOException) File(java.io.File)

Example 2 with LedgerOffloaderFactory

use of org.apache.bookkeeper.mledger.LedgerOffloaderFactory in project pulsar by apache.

the class PulsarService method createManagedLedgerOffloader.

public synchronized LedgerOffloader createManagedLedgerOffloader(OffloadPoliciesImpl offloadPolicies) throws PulsarServerException {
    try {
        if (StringUtils.isNotBlank(offloadPolicies.getManagedLedgerOffloadDriver())) {
            checkNotNull(offloadPolicies.getOffloadersDirectory(), "Offloader driver is configured to be '%s' but no offloaders directory is configured.", offloadPolicies.getManagedLedgerOffloadDriver());
            Offloaders offloaders = offloadersCache.getOrLoadOffloaders(offloadPolicies.getOffloadersDirectory(), config.getNarExtractionDirectory());
            LedgerOffloaderFactory offloaderFactory = offloaders.getOffloaderFactory(offloadPolicies.getManagedLedgerOffloadDriver());
            try {
                return offloaderFactory.create(offloadPolicies, ImmutableMap.of(LedgerOffloader.METADATA_SOFTWARE_VERSION_KEY.toLowerCase(), PulsarVersion.getVersion(), LedgerOffloader.METADATA_SOFTWARE_GITSHA_KEY.toLowerCase(), PulsarVersion.getGitSha()), schemaStorage, getOffloaderScheduler(offloadPolicies));
            } catch (IOException ioe) {
                throw new PulsarServerException(ioe.getMessage(), ioe.getCause());
            }
        } else {
            LOG.info("No ledger offloader configured, using NULL instance");
            return NullLedgerOffloader.INSTANCE;
        }
    } catch (Throwable t) {
        throw new PulsarServerException(t);
    }
}
Also used : LedgerOffloaderFactory(org.apache.bookkeeper.mledger.LedgerOffloaderFactory) Offloaders(org.apache.bookkeeper.mledger.offload.Offloaders) IOException(java.io.IOException)

Example 3 with LedgerOffloaderFactory

use of org.apache.bookkeeper.mledger.LedgerOffloaderFactory in project pulsar by apache.

the class PulsarConnectorCache method initManagedLedgerOffloader.

private LedgerOffloader initManagedLedgerOffloader(OffloadPoliciesImpl offloadPolicies, PulsarConnectorConfig pulsarConnectorConfig) {
    try {
        if (StringUtils.isNotBlank(offloadPolicies.getManagedLedgerOffloadDriver())) {
            checkNotNull(offloadPolicies.getOffloadersDirectory(), "Offloader driver is configured to be '%s' but no offloaders directory is configured.", offloadPolicies.getManagedLedgerOffloadDriver());
            Offloaders offloaders = offloadersCache.getOrLoadOffloaders(offloadPolicies.getOffloadersDirectory(), pulsarConnectorConfig.getNarExtractionDirectory());
            LedgerOffloaderFactory offloaderFactory = offloaders.getOffloaderFactory(offloadPolicies.getManagedLedgerOffloadDriver());
            try {
                return offloaderFactory.create(offloadPolicies, ImmutableMap.of(LedgerOffloader.METADATA_SOFTWARE_VERSION_KEY.toLowerCase(), PulsarVersion.getVersion(), LedgerOffloader.METADATA_SOFTWARE_GITSHA_KEY.toLowerCase(), PulsarVersion.getGitSha()), getOffloaderScheduler(offloadPolicies));
            } catch (IOException ioe) {
                log.error("Failed to create offloader: ", ioe);
                throw new RuntimeException(ioe.getMessage(), ioe.getCause());
            }
        } else {
            log.info("No ledger offloader configured, using NULL instance");
            return NullLedgerOffloader.INSTANCE;
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : LedgerOffloaderFactory(org.apache.bookkeeper.mledger.LedgerOffloaderFactory) Offloaders(org.apache.bookkeeper.mledger.offload.Offloaders) IOException(java.io.IOException)

Example 4 with LedgerOffloaderFactory

use of org.apache.bookkeeper.mledger.LedgerOffloaderFactory in project pulsar by yahoo.

the class PulsarService method createManagedLedgerOffloader.

public synchronized LedgerOffloader createManagedLedgerOffloader(OffloadPoliciesImpl offloadPolicies) throws PulsarServerException {
    try {
        if (StringUtils.isNotBlank(offloadPolicies.getManagedLedgerOffloadDriver())) {
            checkNotNull(offloadPolicies.getOffloadersDirectory(), "Offloader driver is configured to be '%s' but no offloaders directory is configured.", offloadPolicies.getManagedLedgerOffloadDriver());
            Offloaders offloaders = offloadersCache.getOrLoadOffloaders(offloadPolicies.getOffloadersDirectory(), config.getNarExtractionDirectory());
            LedgerOffloaderFactory offloaderFactory = offloaders.getOffloaderFactory(offloadPolicies.getManagedLedgerOffloadDriver());
            try {
                return offloaderFactory.create(offloadPolicies, ImmutableMap.of(LedgerOffloader.METADATA_SOFTWARE_VERSION_KEY.toLowerCase(), PulsarVersion.getVersion(), LedgerOffloader.METADATA_SOFTWARE_GITSHA_KEY.toLowerCase(), PulsarVersion.getGitSha(), LedgerOffloader.METADATA_PULSAR_CLUSTER_NAME.toLowerCase(), config.getClusterName()), schemaStorage, getOffloaderScheduler(offloadPolicies), this.offloaderStats);
            } catch (IOException ioe) {
                throw new PulsarServerException(ioe.getMessage(), ioe.getCause());
            }
        } else {
            LOG.info("No ledger offloader configured, using NULL instance");
            return NullLedgerOffloader.INSTANCE;
        }
    } catch (Throwable t) {
        throw new PulsarServerException(t);
    }
}
Also used : LedgerOffloaderFactory(org.apache.bookkeeper.mledger.LedgerOffloaderFactory) Offloaders(org.apache.bookkeeper.mledger.offload.Offloaders) IOException(java.io.IOException)

Example 5 with LedgerOffloaderFactory

use of org.apache.bookkeeper.mledger.LedgerOffloaderFactory in project pulsar by yahoo.

the class PulsarConnectorCache method initManagedLedgerOffloader.

private LedgerOffloader initManagedLedgerOffloader(OffloadPoliciesImpl offloadPolicies, PulsarConnectorConfig pulsarConnectorConfig) {
    try {
        if (StringUtils.isNotBlank(offloadPolicies.getManagedLedgerOffloadDriver())) {
            checkNotNull(offloadPolicies.getOffloadersDirectory(), "Offloader driver is configured to be '%s' but no offloaders directory is configured.", offloadPolicies.getManagedLedgerOffloadDriver());
            Offloaders offloaders = offloadersCache.getOrLoadOffloaders(offloadPolicies.getOffloadersDirectory(), pulsarConnectorConfig.getNarExtractionDirectory());
            LedgerOffloaderFactory offloaderFactory = offloaders.getOffloaderFactory(offloadPolicies.getManagedLedgerOffloadDriver());
            try {
                return offloaderFactory.create(offloadPolicies, ImmutableMap.of(LedgerOffloader.METADATA_SOFTWARE_VERSION_KEY.toLowerCase(), PulsarVersion.getVersion(), LedgerOffloader.METADATA_SOFTWARE_GITSHA_KEY.toLowerCase(), PulsarVersion.getGitSha()), this.offloaderScheduler, this.offloaderStats);
            } catch (IOException ioe) {
                log.error("Failed to create offloader: ", ioe);
                throw new RuntimeException(ioe.getMessage(), ioe.getCause());
            }
        } else {
            log.info("No ledger offloader configured, using NULL instance");
            return NullLedgerOffloader.INSTANCE;
        }
    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}
Also used : LedgerOffloaderFactory(org.apache.bookkeeper.mledger.LedgerOffloaderFactory) Offloaders(org.apache.bookkeeper.mledger.offload.Offloaders) IOException(java.io.IOException)

Aggregations

LedgerOffloaderFactory (org.apache.bookkeeper.mledger.LedgerOffloaderFactory)12 IOException (java.io.IOException)9 Offloaders (org.apache.bookkeeper.mledger.offload.Offloaders)6 NarClassLoader (org.apache.pulsar.common.nar.NarClassLoader)6 File (java.io.File)3 Path (java.nio.file.Path)3 CompletableFuture (java.util.concurrent.CompletableFuture)3