1- /**
1+ /*
22 * Licensed to the Apache Software Foundation (ASF) under one
33 * or more contributor license agreements. See the NOTICE file
44 * distributed with this work for additional information
2020import static org .junit .Assert .assertEquals ;
2121import static org .junit .Assert .assertNotNull ;
2222import static org .junit .Assert .assertNull ;
23+ import static org .mockito .Mockito .mock ;
24+ import static org .mockito .Mockito .when ;
2325
2426import java .io .IOException ;
27+ import java .util .ArrayList ;
2528import java .util .OptionalLong ;
2629import java .util .concurrent .ExecutorService ;
2730import java .util .concurrent .Executors ;
3134import org .apache .hadoop .conf .Configuration ;
3235import org .apache .hadoop .fs .FileSystem ;
3336import org .apache .hadoop .fs .Path ;
37+ import org .apache .hadoop .hbase .Cell ;
3438import org .apache .hadoop .hbase .HBaseClassTestRule ;
3539import org .apache .hadoop .hbase .HBaseConfiguration ;
3640import org .apache .hadoop .hbase .HBaseTestingUtility ;
4852import org .apache .hadoop .hbase .replication .ReplicationPeer ;
4953import org .apache .hadoop .hbase .replication .ReplicationPeerConfig ;
5054import org .apache .hadoop .hbase .replication .ReplicationQueueStorage ;
51- import org .apache .hadoop .hbase .replication .regionserver .HBaseInterClusterReplicationEndpoint ;
52- import org .apache .hadoop .hbase .replication .regionserver .RecoveredReplicationSource ;
53- import org .apache .hadoop .hbase .replication .regionserver .RecoveredReplicationSourceShipper ;
54- import org .apache .hadoop .hbase .replication .regionserver .Replication ;
55- import org .apache .hadoop .hbase .replication .regionserver .ReplicationSource ;
56- import org .apache .hadoop .hbase .replication .regionserver .ReplicationSourceManager ;
5755import org .apache .hadoop .hbase .testclassification .MediumTests ;
5856import org .apache .hadoop .hbase .testclassification .ReplicationTests ;
5957import org .apache .hadoop .hbase .util .Bytes ;
@@ -142,7 +140,7 @@ public void testLogMoving() throws Exception{
142140 entry = reader .next ();
143141 assertNotNull (entry );
144142
145- entry = reader .next ();
143+ reader .next ();
146144 entry = reader .next ();
147145
148146 assertNull (entry );
@@ -168,12 +166,12 @@ protected void doStop() {
168166 }
169167 };
170168 replicationEndpoint .start ();
171- ReplicationPeer mockPeer = Mockito . mock (ReplicationPeer .class );
172- Mockito . when (mockPeer .getPeerBandwidth ()).thenReturn (0L );
169+ ReplicationPeer mockPeer = mock (ReplicationPeer .class );
170+ when (mockPeer .getPeerBandwidth ()).thenReturn (0L );
173171 Configuration testConf = HBaseConfiguration .create ();
174172 testConf .setInt ("replication.source.maxretriesmultiplier" , 1 );
175- ReplicationSourceManager manager = Mockito . mock (ReplicationSourceManager .class );
176- Mockito . when (manager .getTotalBufferUsed ()).thenReturn (new AtomicLong ());
173+ ReplicationSourceManager manager = mock (ReplicationSourceManager .class );
174+ when (manager .getTotalBufferUsed ()).thenReturn (new AtomicLong ());
177175 source .init (testConf , null , manager , null , mockPeer , null , "testPeer" , null ,
178176 p -> OptionalLong .empty (), null );
179177 ExecutorService executor = Executors .newSingleThreadExecutor ();
@@ -194,6 +192,47 @@ public boolean evaluate() throws Exception {
194192 });
195193 }
196194
195+ @ Test
196+ public void testTerminateClearsBuffer () throws Exception {
197+ ReplicationSource source = new ReplicationSource ();
198+ ReplicationSourceManager mockManager = mock (ReplicationSourceManager .class );
199+ MetricsReplicationGlobalSourceSource mockMetrics =
200+ mock (MetricsReplicationGlobalSourceSource .class );
201+ AtomicLong buffer = new AtomicLong ();
202+ when (mockManager .getTotalBufferUsed ()).thenReturn (buffer );
203+ when (mockManager .getGlobalMetrics ()).thenReturn (mockMetrics );
204+ ReplicationPeer mockPeer = mock (ReplicationPeer .class );
205+ when (mockPeer .getPeerBandwidth ()).thenReturn (0L );
206+ Configuration testConf = HBaseConfiguration .create ();
207+ source .init (testConf , null , mockManager , null , mockPeer , null ,
208+ "testPeer" , null , p -> OptionalLong .empty (), mock (MetricsSource .class ));
209+ ReplicationSourceWALReader reader = new ReplicationSourceWALReader (null ,
210+ conf , null , 0 , null , source );
211+ ReplicationSourceShipper shipper =
212+ new ReplicationSourceShipper (conf , null , null , source );
213+ shipper .entryReader = reader ;
214+ source .workerThreads .put ("testPeer" , shipper );
215+ WALEntryBatch batch = new WALEntryBatch (10 , logDir );
216+ WAL .Entry mockEntry = mock (WAL .Entry .class );
217+ WALEdit mockEdit = mock (WALEdit .class );
218+ WALKeyImpl mockKey = mock (WALKeyImpl .class );
219+ when (mockEntry .getEdit ()).thenReturn (mockEdit );
220+ when (mockEdit .isEmpty ()).thenReturn (false );
221+ when (mockEntry .getKey ()).thenReturn (mockKey );
222+ when (mockKey .estimatedSerializedSizeOf ()).thenReturn (1000L );
223+ when (mockEdit .heapSize ()).thenReturn (10000L );
224+ when (mockEdit .size ()).thenReturn (0 );
225+ ArrayList <Cell > cells = new ArrayList <>();
226+ KeyValue kv = new KeyValue (Bytes .toBytes ("0001" ), Bytes .toBytes ("f" ),
227+ Bytes .toBytes ("1" ), Bytes .toBytes ("v1" ));
228+ cells .add (kv );
229+ when (mockEdit .getCells ()).thenReturn (cells );
230+ reader .addEntryToBatch (batch , mockEntry );
231+ reader .entryBatchQueue .put (batch );
232+ source .terminate ("test" );
233+ assertEquals (0 , source .getSourceManager ().getTotalBufferUsed ().get ());
234+ }
235+
197236 /**
198237 * Tests that recovered queues are preserved on a regionserver shutdown.
199238 * See HBASE-18192
@@ -303,15 +342,15 @@ public void testRecoveredReplicationSourceShipperGetPosition() throws Exception
303342 ServerName deadServer = ServerName .valueOf ("www.deadServer.com" , 12006 , 1524679704419L );
304343 PriorityBlockingQueue <Path > queue = new PriorityBlockingQueue <>();
305344 queue .put (new Path ("/www/html/test" ));
306- RecoveredReplicationSource source = Mockito . mock (RecoveredReplicationSource .class );
307- Server server = Mockito . mock (Server .class );
308- Mockito . when (server .getServerName ()).thenReturn (serverName );
309- Mockito . when (source .getServer ()).thenReturn (server );
310- Mockito . when (source .getServerWALsBelongTo ()).thenReturn (deadServer );
311- ReplicationQueueStorage storage = Mockito . mock (ReplicationQueueStorage .class );
312- Mockito . when (storage .getWALPosition (Mockito .eq (serverName ), Mockito .any (), Mockito .any ()))
345+ RecoveredReplicationSource source = mock (RecoveredReplicationSource .class );
346+ Server server = mock (Server .class );
347+ when (server .getServerName ()).thenReturn (serverName );
348+ when (source .getServer ()).thenReturn (server );
349+ when (source .getServerWALsBelongTo ()).thenReturn (deadServer );
350+ ReplicationQueueStorage storage = mock (ReplicationQueueStorage .class );
351+ when (storage .getWALPosition (Mockito .eq (serverName ), Mockito .any (), Mockito .any ()))
313352 .thenReturn (1001L );
314- Mockito . when (storage .getWALPosition (Mockito .eq (deadServer ), Mockito .any (), Mockito .any ()))
353+ when (storage .getWALPosition (Mockito .eq (deadServer ), Mockito .any (), Mockito .any ()))
315354 .thenReturn (-1L );
316355 conf .setInt ("replication.source.maxretriesmultiplier" , -1 );
317356 RecoveredReplicationSourceShipper shipper =
0 commit comments