Search in sources :

Example 1 with MemLeaseManager

use of com.demandware.carbonj.service.engine.kinesis.kcl.MemLeaseManager in project carbonj by salesforce.

the class KinesisConsumer method run.

public void run() {
    while (!closed) {
        try {
            AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
            String workerId = kinesisApplicationName + "-worker";
            if (kinesisConfig.isRecoveryEnabled()) {
                initCatchupKinesisClient();
            }
            KinesisClientLibConfiguration kinesisClientLibConfiguration = new KinesisClientLibConfiguration(kinesisApplicationName, kinesisStreamName, credentialsProvider, workerId).withInitialPositionInStream(InitialPositionInStream.LATEST).withFailoverTimeMillis(kinesisConfig.getLeaseExpirationTimeInSecs() * 1000).withRegionName(kinesisConsumerRegion);
            int maxRecords = kinesisConfig.getMaxRecords();
            if (maxRecords > 0) {
                kinesisClientLibConfiguration.withMaxRecords(maxRecords);
            }
            log.info(" Kinesis Client Library started with application name " + kinesisApplicationName + " with stream " + kinesisStreamName + " and worker id is " + workerId);
            IRecordProcessorFactory recordProcessorFactory = new KinesisRecordProcessorFactory(metricRegistry, pointProcessor, kinesisConfig, kinesisStreamName);
            worker = new Worker.Builder().recordProcessorFactory(recordProcessorFactory).config(kinesisClientLibConfiguration).leaseManager(new MemLeaseManager(kinesisConfig.getLeaseTakerDelayInMillis())).build();
            worker.run();
        } catch (Throwable t) {
            log.error("Error in initializing kinesis consumer", t);
            shutdownQuietly(worker);
            try {
                Thread.sleep(TimeUnit.SECONDS.toMillis(kinesisConfig.getInitRetryTimeInSecs()));
            } catch (InterruptedException e) {
                log.error("wait interrupted", e);
            }
            noOfRestarts.inc();
        }
    }
}
Also used : DefaultAWSCredentialsProviderChain(com.amazonaws.auth.DefaultAWSCredentialsProviderChain) MemLeaseManager(com.demandware.carbonj.service.engine.kinesis.kcl.MemLeaseManager) IRecordProcessorFactory(com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorFactory) KinesisClientLibConfiguration(com.amazonaws.services.kinesis.clientlibrary.lib.worker.KinesisClientLibConfiguration) Worker(com.amazonaws.services.kinesis.clientlibrary.lib.worker.Worker) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider)

Example 2 with MemLeaseManager

use of com.demandware.carbonj.service.engine.kinesis.kcl.MemLeaseManager in project carbonj by salesforce.

the class TestMemLeaseManager method testBasicPath.

@Test
public void testBasicPath() throws Exception {
    ILeaseManager<KinesisClientLease> leaseManager = new MemLeaseManager<>(1);
    // check if the lease table exists
    Assert.assertFalse(leaseManager.leaseTableExists());
    // create table if it not exists
    Assert.assertTrue(leaseManager.createLeaseTableIfNotExists(10L, 10L));
    // check if the lease table exists
    Assert.assertTrue(leaseManager.waitUntilLeaseTableExists(1, 10));
    // check if the lease table exists
    Assert.assertTrue(leaseManager.leaseTableExists());
    // check if the lease table is empty
    Assert.assertTrue(leaseManager.isLeaseTableEmpty());
    Assert.assertEquals(0, leaseManager.listLeases().size());
    // create lease for 5 shards
    for (int i = 0; i < NO_OF_SHARDS; i++) {
        String shardId = "shard-" + i;
        KinesisClientLease lease = newKCLLease(shardId);
        Assert.assertTrue(leaseManager.createLeaseIfNotExists(lease));
    }
    // check if the lease table is not empty
    Assert.assertFalse(leaseManager.isLeaseTableEmpty());
    Assert.assertEquals(5, leaseManager.listLeases().size());
    verify(leaseManager, null, 0L);
    for (int i = 0; i < NO_OF_SHARDS; i++) {
        String shardId = "shard-" + i;
        KinesisClientLease lease = leaseManager.getLease(shardId);
        leaseManager.takeLease(lease, OWNER);
    }
    verify(leaseManager, OWNER, 1L);
    renewLeases(leaseManager);
    verify(leaseManager, OWNER, 2L);
    updateLeases(leaseManager);
    verify(leaseManager, OWNER, 3L);
}
Also used : MemLeaseManager(com.demandware.carbonj.service.engine.kinesis.kcl.MemLeaseManager) KinesisClientLease(com.amazonaws.services.kinesis.leases.impl.KinesisClientLease) Test(org.junit.Test)

Aggregations

MemLeaseManager (com.demandware.carbonj.service.engine.kinesis.kcl.MemLeaseManager)2 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)1 DefaultAWSCredentialsProviderChain (com.amazonaws.auth.DefaultAWSCredentialsProviderChain)1 IRecordProcessorFactory (com.amazonaws.services.kinesis.clientlibrary.interfaces.IRecordProcessorFactory)1 KinesisClientLibConfiguration (com.amazonaws.services.kinesis.clientlibrary.lib.worker.KinesisClientLibConfiguration)1 Worker (com.amazonaws.services.kinesis.clientlibrary.lib.worker.Worker)1 KinesisClientLease (com.amazonaws.services.kinesis.leases.impl.KinesisClientLease)1 Test (org.junit.Test)1