Commit 637ad6cac5144ce205d0c9003b1a74813e045a96
1 parent
24a31d61
remove boundStatementBuilder from doSavePartition method
Showing
2 changed files
with
31 additions
and
30 deletions
... | ... | @@ -472,15 +472,15 @@ public class CassandraBaseTimeseriesDao extends AbstractCassandraBaseTimeseriesD |
472 | 472 | |
473 | 473 | private ListenableFuture<Integer> doSavePartition(TenantId tenantId, EntityId entityId, String key, long ttl, long partition) { |
474 | 474 | log.debug("Saving partition {} for the entity [{}-{}] and key {}", partition, entityId.getEntityType(), entityId.getId(), key); |
475 | - BoundStatementBuilder stmtBuilder = new BoundStatementBuilder((ttl == 0 ? getPartitionInsertStmt() : getPartitionInsertTtlStmt()).bind()); | |
476 | - stmtBuilder.setString(0, entityId.getEntityType().name()) | |
475 | + PreparedStatement preparedStatement = ttl == 0 ? getPartitionInsertStmt() : getPartitionInsertTtlStmt(); | |
476 | + BoundStatement stmt = preparedStatement.bind(); | |
477 | + stmt = stmt.setString(0, entityId.getEntityType().name()) | |
477 | 478 | .setUuid(1, entityId.getId()) |
478 | 479 | .setLong(2, partition) |
479 | 480 | .setString(3, key); |
480 | 481 | if (ttl > 0) { |
481 | - stmtBuilder.setInt(4, (int) ttl); | |
482 | + stmt = stmt.setInt(4, (int) ttl); | |
482 | 483 | } |
483 | - BoundStatement stmt = stmtBuilder.build(); | |
484 | 484 | return getFuture(executeAsyncWrite(tenantId, stmt), rs -> 0); |
485 | 485 | } |
486 | 486 | ... | ... |
... | ... | @@ -21,10 +21,10 @@ import com.datastax.oss.driver.api.core.cql.PreparedStatement; |
21 | 21 | import com.datastax.oss.driver.api.core.cql.Statement; |
22 | 22 | import com.google.common.util.concurrent.Futures; |
23 | 23 | import org.junit.Before; |
24 | -import org.junit.Ignore; | |
25 | 24 | import org.junit.Test; |
26 | 25 | import org.junit.runner.RunWith; |
27 | 26 | import org.mockito.Mock; |
27 | +import org.mockito.Spy; | |
28 | 28 | import org.mockito.runners.MockitoJUnitRunner; |
29 | 29 | import org.springframework.core.env.Environment; |
30 | 30 | import org.springframework.test.util.ReflectionTestUtils; |
... | ... | @@ -36,9 +36,9 @@ import org.thingsboard.server.dao.timeseries.CassandraBaseTimeseriesDao; |
36 | 36 | import java.util.UUID; |
37 | 37 | |
38 | 38 | import static org.mockito.Matchers.any; |
39 | +import static org.mockito.Matchers.anyInt; | |
39 | 40 | import static org.mockito.Matchers.anyString; |
40 | 41 | import static org.mockito.Mockito.doReturn; |
41 | -import static org.mockito.Mockito.spy; | |
42 | 42 | import static org.mockito.Mockito.times; |
43 | 43 | import static org.mockito.Mockito.verify; |
44 | 44 | import static org.mockito.Mockito.when; |
... | ... | @@ -46,36 +46,29 @@ import static org.mockito.Mockito.when; |
46 | 46 | @RunWith(MockitoJUnitRunner.class) |
47 | 47 | public class CassandraPartitionsCacheTest { |
48 | 48 | |
49 | + @Spy | |
49 | 50 | private CassandraBaseTimeseriesDao cassandraBaseTimeseriesDao; |
50 | 51 | |
51 | 52 | @Mock |
52 | - private Environment environment; | |
53 | + private PreparedStatement preparedStatement; | |
53 | 54 | |
54 | 55 | @Mock |
55 | - private CassandraBufferedRateExecutor rateLimiter; | |
56 | + private BoundStatement boundStatement; | |
56 | 57 | |
57 | 58 | @Mock |
58 | - private CassandraCluster cluster; | |
59 | + private Environment environment; | |
59 | 60 | |
60 | 61 | @Mock |
61 | - private GuavaSession session; | |
62 | + private CassandraBufferedRateExecutor rateLimiter; | |
62 | 63 | |
63 | 64 | @Mock |
64 | - private PreparedStatement preparedStatement; | |
65 | + private CassandraCluster cluster; | |
65 | 66 | |
66 | 67 | @Mock |
67 | - private BoundStatement boundStatement; | |
68 | + private GuavaSession session; | |
68 | 69 | |
69 | 70 | @Before |
70 | 71 | public void setUp() throws Exception { |
71 | - when(cluster.getDefaultReadConsistencyLevel()).thenReturn(ConsistencyLevel.ONE); | |
72 | - when(cluster.getDefaultWriteConsistencyLevel()).thenReturn(ConsistencyLevel.ONE); | |
73 | - when(cluster.getSession()).thenReturn(session); | |
74 | - when(session.prepare(anyString())).thenReturn(preparedStatement); | |
75 | - when(preparedStatement.bind()).thenReturn(boundStatement); | |
76 | - | |
77 | - cassandraBaseTimeseriesDao = spy(new CassandraBaseTimeseriesDao()); | |
78 | - | |
79 | 72 | ReflectionTestUtils.setField(cassandraBaseTimeseriesDao, "partitioning", "MONTHS"); |
80 | 73 | ReflectionTestUtils.setField(cassandraBaseTimeseriesDao, "partitionsCacheSize", 100000); |
81 | 74 | ReflectionTestUtils.setField(cassandraBaseTimeseriesDao, "systemTtl", 0); |
... | ... | @@ -84,10 +77,20 @@ public class CassandraPartitionsCacheTest { |
84 | 77 | ReflectionTestUtils.setField(cassandraBaseTimeseriesDao, "rateLimiter", rateLimiter); |
85 | 78 | ReflectionTestUtils.setField(cassandraBaseTimeseriesDao, "cluster", cluster); |
86 | 79 | |
80 | + when(cluster.getDefaultReadConsistencyLevel()).thenReturn(ConsistencyLevel.ONE); | |
81 | + when(cluster.getDefaultWriteConsistencyLevel()).thenReturn(ConsistencyLevel.ONE); | |
82 | + when(cluster.getSession()).thenReturn(session); | |
83 | + when(session.prepare(anyString())).thenReturn(preparedStatement); | |
84 | + | |
85 | + when(preparedStatement.bind()).thenReturn(boundStatement); | |
86 | + | |
87 | + when(boundStatement.setString(anyInt(), anyString())).thenReturn(boundStatement); | |
88 | + when(boundStatement.setUuid(anyInt(), any(UUID.class))).thenReturn(boundStatement); | |
89 | + when(boundStatement.setLong(anyInt(), any(Long.class))).thenReturn(boundStatement); | |
90 | + | |
87 | 91 | doReturn(Futures.immediateFuture(null)).when(cassandraBaseTimeseriesDao).getFuture(any(TbResultSetFuture.class), any()); |
88 | 92 | } |
89 | 93 | |
90 | - @Ignore | |
91 | 94 | @Test |
92 | 95 | public void testPartitionSave() throws Exception { |
93 | 96 | cassandraBaseTimeseriesDao.init(); |
... | ... | @@ -96,14 +99,12 @@ public class CassandraPartitionsCacheTest { |
96 | 99 | TenantId tenantId = new TenantId(id); |
97 | 100 | long tsKvEntryTs = System.currentTimeMillis(); |
98 | 101 | |
99 | -// for (int i = 0; i < 50000; i++) { | |
100 | -// cassandraBaseTimeseriesDao.savePartition(tenantId, tenantId, tsKvEntryTs, "test" + i, 0); | |
101 | -// } | |
102 | -// | |
103 | -// for (int i = 0; i < 60000; i++) { | |
104 | -// cassandraBaseTimeseriesDao.savePartition(tenantId, tenantId, tsKvEntryTs, "test" + i, 0); | |
105 | -// } | |
106 | - cassandraBaseTimeseriesDao.savePartition(tenantId, tenantId, tsKvEntryTs, "test", 0); | |
107 | - verify(cassandraBaseTimeseriesDao, times(1)).executeAsyncWrite(any(TenantId.class), any(Statement.class)); | |
102 | + for (int i = 0; i < 50000; i++) { | |
103 | + cassandraBaseTimeseriesDao.savePartition(tenantId, tenantId, tsKvEntryTs, "test" + i, 0); | |
104 | + } | |
105 | + for (int i = 0; i < 60000; i++) { | |
106 | + cassandraBaseTimeseriesDao.savePartition(tenantId, tenantId, tsKvEntryTs, "test" + i, 0); | |
107 | + } | |
108 | + verify(cassandraBaseTimeseriesDao, times(60000)).executeAsyncWrite(any(TenantId.class), any(Statement.class)); | |
108 | 109 | } |
109 | 110 | } |
\ No newline at end of file | ... | ... |