Search in sources :

Example 11 with MemoryEventStoreWithBuffer

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

the class MemoryEventStoreRollbackAndAckTest method testRollback.

@Test
public void testRollback() {
    int bufferSize = 16;
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(bufferSize);
    eventStore.start();
    for (int i = 0; i < bufferSize / 2; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + i));
        sleep(100L);
        Assert.assertTrue(result);
    }
    sleep(50L);
    Position first = eventStore.getFirstPosition();
    Position lastest = eventStore.getLatestPosition();
    Assert.assertEquals(first, CanalEventUtils.createPosition(buildEvent("1", 1L, 1L)));
    Assert.assertEquals(lastest, CanalEventUtils.createPosition(buildEvent("1", 1L, 1L + bufferSize / 2 - 1)));
    System.out.println("start get");
    Events<Event> entrys1 = eventStore.tryGet(first, bufferSize);
    System.out.println("first get size : " + entrys1.getEvents().size());
    eventStore.rollback();
    entrys1 = eventStore.tryGet(first, bufferSize);
    System.out.println("after rollback get size : " + entrys1.getEvents().size());
    Assert.assertTrue(entrys1.getEvents().size() == bufferSize / 2);
    // 继续造数据
    for (int i = bufferSize / 2; i < bufferSize; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + i));
        sleep(100L);
        Assert.assertTrue(result);
    }
    Events<Event> entrys2 = eventStore.tryGet(entrys1.getPositionRange().getEnd(), bufferSize);
    System.out.println("second get size : " + entrys2.getEvents().size());
    eventStore.rollback();
    entrys2 = eventStore.tryGet(entrys1.getPositionRange().getEnd(), bufferSize);
    System.out.println("after rollback get size : " + entrys2.getEvents().size());
    Assert.assertTrue(entrys2.getEvents().size() == bufferSize);
    first = eventStore.getFirstPosition();
    lastest = eventStore.getLatestPosition();
    List<Event> entrys = new ArrayList<Event>(entrys2.getEvents());
    Assert.assertTrue(entrys.size() == bufferSize);
    Assert.assertEquals(first, entrys2.getPositionRange().getStart());
    Assert.assertEquals(lastest, entrys2.getPositionRange().getEnd());
    Assert.assertEquals(first, CanalEventUtils.createPosition(entrys.get(0)));
    Assert.assertEquals(lastest, CanalEventUtils.createPosition(entrys.get(bufferSize - 1)));
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) Position(com.alibaba.otter.canal.protocol.position.Position) ArrayList(java.util.ArrayList) Event(com.alibaba.otter.canal.store.model.Event) Test(org.junit.Test)

Example 12 with MemoryEventStoreWithBuffer

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

the class MemoryEventStoreMemBatchTest method testOnePut.

@Test
public void testOnePut() {
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBatchMode(BatchMode.MEMSIZE);
    eventStore.start();
    // 尝试阻塞
    try {
        eventStore.put(buildEvent("1", 1L, 1L, 1024));
    } 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)

Example 13 with MemoryEventStoreWithBuffer

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

the class MemoryEventStoreMemBatchTest method testRollback.

@Test
public void testRollback() {
    int bufferSize = 16;
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(bufferSize);
    eventStore.setBatchMode(BatchMode.MEMSIZE);
    eventStore.start();
    for (int i = 0; i < bufferSize / 2; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + i));
        sleep(100L);
        Assert.assertTrue(result);
    }
    sleep(50L);
    Position first = eventStore.getFirstPosition();
    Position lastest = eventStore.getLatestPosition();
    Assert.assertEquals(first, CanalEventUtils.createPosition(buildEvent("1", 1L, 1L)));
    Assert.assertEquals(lastest, CanalEventUtils.createPosition(buildEvent("1", 1L, 1L + bufferSize / 2 - 1)));
    System.out.println("start get");
    Events<Event> entrys1 = eventStore.tryGet(first, bufferSize);
    System.out.println("first get size : " + entrys1.getEvents().size());
    eventStore.rollback();
    entrys1 = eventStore.tryGet(first, bufferSize);
    System.out.println("after rollback get size : " + entrys1.getEvents().size());
    Assert.assertTrue(entrys1.getEvents().size() == bufferSize / 2);
    // 继续造数据
    for (int i = bufferSize / 2; i < bufferSize; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + i));
        sleep(100L);
        Assert.assertTrue(result);
    }
    Events<Event> entrys2 = eventStore.tryGet(entrys1.getPositionRange().getEnd(), bufferSize);
    System.out.println("second get size : " + entrys2.getEvents().size());
    eventStore.rollback();
    entrys2 = eventStore.tryGet(entrys1.getPositionRange().getEnd(), bufferSize);
    System.out.println("after rollback get size : " + entrys2.getEvents().size());
    Assert.assertTrue(entrys2.getEvents().size() == bufferSize);
    first = eventStore.getFirstPosition();
    lastest = eventStore.getLatestPosition();
    List<Event> entrys = new ArrayList<Event>(entrys2.getEvents());
    Assert.assertTrue(entrys.size() == bufferSize);
    Assert.assertEquals(first, entrys2.getPositionRange().getStart());
    Assert.assertEquals(lastest, entrys2.getPositionRange().getEnd());
    Assert.assertEquals(first, CanalEventUtils.createPosition(entrys.get(0)));
    Assert.assertEquals(lastest, CanalEventUtils.createPosition(entrys.get(bufferSize - 1)));
    eventStore.stop();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) Position(com.alibaba.otter.canal.protocol.position.Position) ArrayList(java.util.ArrayList) Event(com.alibaba.otter.canal.store.model.Event) Test(org.junit.Test)

Example 14 with MemoryEventStoreWithBuffer

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

the class MemoryEventStoreMultiThreadTest method setUp.

@Before
public void setUp() {
    eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(16 * 16);
    eventStore.setBatchMode(BatchMode.MEMSIZE);
    eventStore.start();
}
Also used : MemoryEventStoreWithBuffer(com.alibaba.otter.canal.store.memory.MemoryEventStoreWithBuffer) Before(org.junit.Before)

Example 15 with MemoryEventStoreWithBuffer

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

the class MemoryEventStorePutAndGetTest method testFullPutBatchGet.

@Test
public void testFullPutBatchGet() {
    int bufferSize = 16;
    MemoryEventStoreWithBuffer eventStore = new MemoryEventStoreWithBuffer();
    eventStore.setBufferSize(bufferSize);
    eventStore.start();
    for (int i = 0; i < bufferSize; i++) {
        boolean result = eventStore.tryPut(buildEvent("1", 1L, 1L + i));
        sleep(100L);
        Assert.assertTrue(result);
    }
    Position first = eventStore.getFirstPosition();
    Position lastest = eventStore.getLatestPosition();
    Assert.assertEquals(first, CanalEventUtils.createPosition(buildEvent("1", 1L, 1L)));
    Assert.assertEquals(lastest, CanalEventUtils.createPosition(buildEvent("1", 1L, 1L + bufferSize - 1)));
    System.out.println("start get");
    Events<Event> entrys1 = eventStore.tryGet(first, bufferSize);
    System.out.println("first get size : " + entrys1.getEvents().size());
    Assert.assertTrue(entrys1.getEvents().size() == bufferSize);
    Assert.assertEquals(first, entrys1.getPositionRange().getStart());
    Assert.assertEquals(lastest, entrys1.getPositionRange().getEnd());
    Assert.assertEquals(first, CanalEventUtils.createPosition(entrys1.getEvents().get(0)));
    Assert.assertEquals(lastest, CanalEventUtils.createPosition(entrys1.getEvents().get(bufferSize - 1)));
    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)

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