Search in sources :

Example 6 with MemoryEventStoreWithBuffer

use of com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer in project canal by alibaba.

the class MemoryEventStoreMemBatchTest method testBlockPutOneGet.

@Test
public void testBlockPutOneGet() {
    final MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(16);
    eventStore.setBatchMode(BatchMode.MEMSIZE);
    eventStore.start();
    final int batchSize = 10;
    for (int i = 0; i < batchSize; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L));
        Assert.assertTrue(result);
    }
    final Position position = eventStore.getFirstPosition();
    try {
        Events<Event> entrys = eventStore.get(position, batchSize);
        Assert.assertTrue(entrys.getEvents().size() == batchSize);
        Assert.assertEquals(position, entrys.getPositionRange().getStart());
        Assert.assertEquals(position, entrys.getPositionRange().getEnd());
    } catch (CanalStoreException e) {
    } catch (InterruptedException e) {
    }
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(new Runnable() {

        public void run() {
            boolean result = false;
            try {
                eventStore.get(position, batchSize);
            } catch (CanalStoreException e) {
            } catch (InterruptedException e) {
                System.out.println("interrupt occured.");
                result = true;
            }
            Assert.assertTrue(result);
        }
    });
    try {
        Thread.sleep(1000L);
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    executor.shutdownNow();
    try {
        Thread.sleep(1000L);
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) Position(com.alibaba.otter.canal.protocol.position.Position) ExecutorService(java.util.concurrent.ExecutorService) Event(com.alibaba.otter.canal.store.model.Event) CanalStoreException(com.alibaba.otter.canal.store.CanalStoreException) Test(org.junit.Test)

Example 7 with MemoryEventStoreWithBuffer

use of com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer in project canal by alibaba.

the class MemoryEventStoreMemBatchTest method testOnePutExceedLimit.

@Test
public void testOnePutExceedLimit() {
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(1);
    eventStore.setBatchMode(BatchMode.MEMSIZE);
    eventStore.start();
    // 尝试阻塞
    try {
        // 只有一条记录,第一条超过也允许放入
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L, 1025));
        Assert.assertTrue(result);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) CanalStoreException(com.alibaba.otter.canal.store.CanalStoreException) Test(org.junit.Test)

Example 8 with MemoryEventStoreWithBuffer

use of com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer in project canal by alibaba.

the class MemoryEventStorePutAndGetTest method testOnePutOneGet.

@Test
public void testOnePutOneGet() {
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.start();
    boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L));
    Assert.assertTrue(result);
    Position position = eventStore.getFirstPosition();
    Events<Event> entrys = eventStore.tryGet(position, 1);
    Assert.assertTrue(entrys.getEvents().size() == 1);
    Assert.assertEquals(position, entrys.getPositionRange().getStart());
    Assert.assertEquals(position, entrys.getPositionRange().getEnd());
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) Position(com.alibaba.otter.canal.protocol.position.Position) Event(com.alibaba.otter.canal.store.model.Event) Test(org.junit.Test)

Example 9 with MemoryEventStoreWithBuffer

use of com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer in project canal by alibaba.

the class MemoryEventStorePutAndGetTest method testBlockPutOneGet.

@Test
public void testBlockPutOneGet() {
    final MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.start();
    final int batchSize = 10;
    for (int i = 0; i < batchSize; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L));
        Assert.assertTrue(result);
    }
    final Position position = eventStore.getFirstPosition();
    try {
        Events<Event> entrys = eventStore.get(position, batchSize);
        Assert.assertTrue(entrys.getEvents().size() == batchSize);
        Assert.assertEquals(position, entrys.getPositionRange().getStart());
        Assert.assertEquals(position, entrys.getPositionRange().getEnd());
    } catch (CanalStoreException e) {
    } catch (InterruptedException e) {
    }
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(new Runnable() {

        public void run() {
            boolean result = false;
            try {
                eventStore.get(position, batchSize);
            } catch (CanalStoreException e) {
            } catch (InterruptedException e) {
                System.out.println("interrupt occured.");
                result = true;
            }
            Assert.assertTrue(result);
        }
    });
    try {
        Thread.sleep(1000L);
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    executor.shutdownNow();
    try {
        Thread.sleep(1000L);
    } catch (InterruptedException e) {
        Assert.fail(e.getMessage());
    }
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) Position(com.alibaba.otter.canal.protocol.position.Position) ExecutorService(java.util.concurrent.ExecutorService) Event(com.alibaba.otter.canal.store.model.Event) CanalStoreException(com.alibaba.otter.canal.store.CanalStoreException) Test(org.junit.Test)

Example 10 with MemoryEventStoreWithBuffer

use of com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer in project canal by alibaba.

the class MemoryEventStorePutAndGetTest method testOnePut.

@Test
public void testOnePut() {
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.start();
    // 尝试阻塞
    try {
        eventStore.put(buildEvent("1", 1L, 1L));
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
    // 尝试阻塞+超时
    boolean result = false;
    try {
        result = eventStore.put(buildEvent("1", 1L, 1L), 1000L, TimeUnit.MILLISECONDS);
        Assert.assertTrue(result);
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
    // 尝试
    result = eventStore.tryPut(buildEvent("1", 1L, 1L));
    Assert.assertTrue(result);
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) CanalStoreException(com.alibaba.otter.canal.store.CanalStoreException) Test(org.junit.Test)

Aggregations

MemoryEventStoreWithBuffer (com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer)17 Test (org.junit.Test)15 Position (com.alibaba.otter.canal.protocol.position.Position)10 Event (com.alibaba.otter.canal.store.model.Event)10 CanalStoreException (com.alibaba.otter.canal.store.CanalStoreException)7 ArrayList (java.util.ArrayList)4 ExecutorService (java.util.concurrent.ExecutorService)2 CanalException (com.alibaba.otter.canal.common.CanalException)1 StorageMode (com.alibaba.otter.canal.instance.manager.model.CanalParameter.StorageMode)1 StorageScavengeMode (com.alibaba.otter.canal.instance.manager.model.CanalParameter.StorageScavengeMode)1 AbstractCanalStoreScavenge (com.alibaba.otter.canal.store.AbstractCanalStoreScavenge)1 Before (org.junit.Before)1