Search in sources :

Example 1 with JavaTableConfig

use of org.apache.samza.config.JavaTableConfig in project samza by apache.

the class TableManager method addTable.

private void addTable(String tableId, Config config) {
    if (tableContexts.containsKey(tableId)) {
        throw new SamzaException("Table " + tableId + " already exists");
    }
    JavaTableConfig tableConfig = new JavaTableConfig(config);
    String providerFactoryClassName = tableConfig.getTableProviderFactory(tableId);
    TableProviderFactory tableProviderFactory = ReflectionUtil.getObj(providerFactoryClassName, TableProviderFactory.class);
    TableCtx ctx = new TableCtx();
    ctx.tableProvider = tableProviderFactory.getTableProvider(tableId);
    tableContexts.put(tableId, ctx);
}
Also used : JavaTableConfig(org.apache.samza.config.JavaTableConfig) SamzaException(org.apache.samza.SamzaException)

Example 2 with JavaTableConfig

use of org.apache.samza.config.JavaTableConfig in project samza by apache.

the class CachingTableProvider method getTable.

@Override
public ReadWriteUpdateTable getTable() {
    Preconditions.checkNotNull(context, String.format("Table %s not initialized", tableId));
    JavaTableConfig tableConfig = new JavaTableConfig(context.getJobContext().getConfig());
    String realTableId = tableConfig.getForTable(tableId, CachingTableDescriptor.REAL_TABLE_ID);
    ReadWriteUpdateTable table = this.context.getTaskContext().getUpdatableTable(realTableId);
    String cacheTableId = tableConfig.getForTable(tableId, CachingTableDescriptor.CACHE_TABLE_ID);
    ReadWriteUpdateTable cache;
    if (cacheTableId != null) {
        cache = this.context.getTaskContext().getUpdatableTable(cacheTableId);
    } else {
        cache = createDefaultCacheTable(realTableId, tableConfig);
        defaultCaches.add(cache);
    }
    boolean isWriteAround = Boolean.parseBoolean(tableConfig.getForTable(tableId, CachingTableDescriptor.WRITE_AROUND));
    CachingTable cachingTable = new CachingTable(tableId, table, cache, isWriteAround);
    cachingTable.init(this.context);
    return cachingTable;
}
Also used : ReadWriteUpdateTable(org.apache.samza.table.ReadWriteUpdateTable) JavaTableConfig(org.apache.samza.config.JavaTableConfig)

Example 3 with JavaTableConfig

use of org.apache.samza.config.JavaTableConfig in project samza by apache.

the class TestTableConfigGenerator method testWithSerdes.

@Test
public void testWithSerdes() {
    List<TableDescriptor> descriptors = Arrays.asList(new MockLocalTableDescriptor("t1", KVSerde.of(new StringSerde(), new IntegerSerde())), new MockLocalTableDescriptor("t2", KVSerde.of(new StringSerde(), new IntegerSerde())));
    Config jobConfig = new MapConfig(TableConfigGenerator.generateSerdeConfig(descriptors));
    JavaTableConfig javaTableConfig = new JavaTableConfig(jobConfig);
    assertNotNull(javaTableConfig.getKeySerde("t1"));
    assertNotNull(javaTableConfig.getMsgSerde("t1"));
    assertNotNull(javaTableConfig.getKeySerde("t2"));
    assertNotNull(javaTableConfig.getMsgSerde("t2"));
    MapConfig tableConfig = new MapConfig(TableConfigGenerator.generate(jobConfig, descriptors));
    javaTableConfig = new JavaTableConfig(tableConfig);
    assertNotNull(javaTableConfig.getTableProviderFactory("t1"));
    assertNotNull(javaTableConfig.getTableProviderFactory("t2"));
}
Also used : StringSerde(org.apache.samza.serializers.StringSerde) Config(org.apache.samza.config.Config) JavaTableConfig(org.apache.samza.config.JavaTableConfig) MapConfig(org.apache.samza.config.MapConfig) JavaTableConfig(org.apache.samza.config.JavaTableConfig) MapConfig(org.apache.samza.config.MapConfig) TableDescriptor(org.apache.samza.table.descriptors.TableDescriptor) LocalTableDescriptor(org.apache.samza.table.descriptors.LocalTableDescriptor) IntegerSerde(org.apache.samza.serializers.IntegerSerde) Test(org.junit.Test)

Example 4 with JavaTableConfig

use of org.apache.samza.config.JavaTableConfig in project samza by apache.

the class RemoteTableProvider method getTable.

@Override
public ReadWriteUpdateTable getTable() {
    Preconditions.checkNotNull(context, String.format("Table %s not initialized", tableId));
    JavaTableConfig tableConfig = new JavaTableConfig(context.getJobContext().getConfig());
    // Read part
    TableReadFunction readFn = deserializeObject(tableConfig, RemoteTableDescriptor.READ_FN);
    RateLimiter rateLimiter = deserializeObject(tableConfig, RemoteTableDescriptor.RATE_LIMITER);
    if (rateLimiter != null) {
        rateLimiter.init(this.context);
    }
    TableRateLimiter.CreditFunction<?, ?> readCreditFn = deserializeObject(tableConfig, RemoteTableDescriptor.READ_CREDIT_FN);
    TableRateLimiter readRateLimiter = rateLimiter != null && rateLimiter.getSupportedTags().contains(RemoteTableDescriptor.RL_READ_TAG) ? new TableRateLimiter(tableId, rateLimiter, readCreditFn, RemoteTableDescriptor.RL_READ_TAG) : null;
    TableRetryPolicy readRetryPolicy = deserializeObject(tableConfig, RemoteTableDescriptor.READ_RETRY_POLICY);
    // Write part
    // Reuse write rate limiter for update
    TableRateLimiter writeRateLimiter = null;
    TableRetryPolicy writeRetryPolicy = null;
    TableWriteFunction writeFn = deserializeObject(tableConfig, RemoteTableDescriptor.WRITE_FN);
    if (writeFn != null) {
        TableRateLimiter.CreditFunction<?, ?> writeCreditFn = deserializeObject(tableConfig, RemoteTableDescriptor.WRITE_CREDIT_FN);
        writeRateLimiter = rateLimiter != null && rateLimiter.getSupportedTags().contains(RemoteTableDescriptor.RL_WRITE_TAG) ? new TableRateLimiter(tableId, rateLimiter, writeCreditFn, RemoteTableDescriptor.RL_WRITE_TAG) : null;
        writeRetryPolicy = deserializeObject(tableConfig, RemoteTableDescriptor.WRITE_RETRY_POLICY);
    }
    if (readRetryPolicy != null || writeRetryPolicy != null) {
        retryExecutor = createRetryExecutor();
    }
    // Optional executor for future callback/completion. Shared by both read and write operations.
    int callbackPoolSize = Integer.parseInt(tableConfig.getForTable(tableId, RemoteTableDescriptor.ASYNC_CALLBACK_POOL_SIZE, "-1"));
    if (callbackPoolSize > 0) {
        callbackExecutors.computeIfAbsent(tableId, (arg) -> Executors.newFixedThreadPool(callbackPoolSize, (runnable) -> {
            Thread thread = new Thread(runnable);
            thread.setName("table-" + tableId + "-async-callback-pool");
            thread.setDaemon(true);
            return thread;
        }));
    }
    boolean isRateLimited = readRateLimiter != null || writeRateLimiter != null;
    if (isRateLimited) {
        rateLimitingExecutors.computeIfAbsent(tableId, (arg) -> Executors.newSingleThreadExecutor(runnable -> {
            Thread thread = new Thread(runnable);
            thread.setName("table-" + tableId + "-async-executor");
            thread.setDaemon(true);
            return thread;
        }));
    }
    BatchProvider batchProvider = deserializeObject(tableConfig, RemoteTableDescriptor.BATCH_PROVIDER);
    if (batchProvider != null) {
        batchExecutors.computeIfAbsent(tableId, (arg) -> Executors.newSingleThreadScheduledExecutor(runnable -> {
            Thread thread = new Thread(runnable);
            thread.setName("table-" + tableId + "-batch-scheduled-executor");
            thread.setDaemon(true);
            return thread;
        }));
    }
    RemoteTable table = new RemoteTable(tableId, readFn, writeFn, readRateLimiter, writeRateLimiter, writeRateLimiter, rateLimitingExecutors.get(tableId), readRetryPolicy, writeRetryPolicy, retryExecutor, batchProvider, batchExecutors.get(tableId), callbackExecutors.get(tableId));
    table.init(this.context);
    tables.add(table);
    return table;
}
Also used : BatchProvider(org.apache.samza.table.batching.BatchProvider) SerdeUtils(org.apache.samza.table.utils.SerdeUtils) TableRetryPolicy(org.apache.samza.table.retry.TableRetryPolicy) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JavaTableConfig(org.apache.samza.config.JavaTableConfig) Executors(java.util.concurrent.Executors) ArrayList(java.util.ArrayList) List(java.util.List) ReadWriteUpdateTable(org.apache.samza.table.ReadWriteUpdateTable) BaseTableProvider(org.apache.samza.table.BaseTableProvider) RateLimiter(org.apache.samza.util.RateLimiter) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Preconditions(com.google.common.base.Preconditions) RemoteTableDescriptor(org.apache.samza.table.descriptors.RemoteTableDescriptor) ExecutorService(java.util.concurrent.ExecutorService) BatchProvider(org.apache.samza.table.batching.BatchProvider) TableRetryPolicy(org.apache.samza.table.retry.TableRetryPolicy) RateLimiter(org.apache.samza.util.RateLimiter) JavaTableConfig(org.apache.samza.config.JavaTableConfig)

Example 5 with JavaTableConfig

use of org.apache.samza.config.JavaTableConfig in project samza by apache.

the class GuavaCacheTableProvider method getTable.

@Override
public ReadWriteUpdateTable getTable() {
    Preconditions.checkNotNull(context, String.format("Table %s not initialized", tableId));
    JavaTableConfig tableConfig = new JavaTableConfig(context.getJobContext().getConfig());
    Cache guavaCache = SerdeUtils.deserialize(GuavaCacheTableDescriptor.GUAVA_CACHE, tableConfig.getForTable(tableId, GuavaCacheTableDescriptor.GUAVA_CACHE));
    GuavaCacheTable table = new GuavaCacheTable(tableId, guavaCache);
    table.init(this.context);
    guavaTables.add(table);
    return table;
}
Also used : JavaTableConfig(org.apache.samza.config.JavaTableConfig) Cache(com.google.common.cache.Cache)

Aggregations

JavaTableConfig (org.apache.samza.config.JavaTableConfig)5 ReadWriteUpdateTable (org.apache.samza.table.ReadWriteUpdateTable)2 Preconditions (com.google.common.base.Preconditions)1 Cache (com.google.common.cache.Cache)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 SamzaException (org.apache.samza.SamzaException)1 Config (org.apache.samza.config.Config)1 MapConfig (org.apache.samza.config.MapConfig)1 IntegerSerde (org.apache.samza.serializers.IntegerSerde)1 StringSerde (org.apache.samza.serializers.StringSerde)1 BaseTableProvider (org.apache.samza.table.BaseTableProvider)1 BatchProvider (org.apache.samza.table.batching.BatchProvider)1 LocalTableDescriptor (org.apache.samza.table.descriptors.LocalTableDescriptor)1 RemoteTableDescriptor (org.apache.samza.table.descriptors.RemoteTableDescriptor)1