auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
private void setupDynamicMdcFieldTypes(PropertyProvider propertyProvider) { int fieldNumber = 0; while (true) { START final String property = propertyProvider.getProperty(PropertyProvider.PROPERTY_DYNAMIC_MDC_FIELD_TYPES + fieldNumber); END if (null == property) { break; } final int index = property.indexOf('='); if (-1 != index) { String field = property.substring(0, index); String type = property.substring(index + 1); setDynamicMdcFieldType(field, type); } fieldNumber++; } } Nit: No need for `final`.

Target:
private void setupDynamicMdcFieldTypes(PropertyProvider propertyProvider) { int fieldNumber = 0; while (true) { String property = propertyProvider.getProperty(PropertyProvider.PROPERTY_DYNAMIC_MDC_FIELD_TYPES + fieldNumber); if (null == property) { break; } int index = property.indexOf('='); if (-1 != index) { String field = property.substring(0, index); String type = property.substring(index + 1); setDynamicMdcFieldType(field, type); } fieldNumber++; } }
Source:
private String getMatchingDynamicMdcFieldType(String fieldName) { String fieldType = null; START for (final Map.Entry<Pattern, String> entry : dynamicMdcFieldTypes.entrySet()) { END if(entry.getKey().matcher(fieldName).matches()) { fieldType = entry.getValue(); break; } } return fieldType; } Nit: No need for `final`.

Target:
private String getMatchingDynamicMdcFieldType(String fieldName) { String fieldType = null; for (Map.Entry<Pattern, String> entry : dynamicMdcFieldTypes.entrySet()) { if(entry.getKey().matcher(fieldName).matches()) { fieldType = entry.getValue(); break; } } return fieldType; }
Source:
protected boolean connect() throws IOException { START if (isConnected()) { END return false; } if (super.connect()) { this.sslEngine = sslContext.createSSLEngine(); this.sslEngine.setUseClientMode(true); this.sslSession = sslEngine.getSession(); // Begin handshake sslEngine.beginHandshake(); doHandshake(channel(), sslEngine, ByteBuffer.allocate(sslSession.getPacketBufferSize()), ByteBuffer.allocate(sslSession.getPacketBufferSize())); } return false; } This check is superfluous as `super.connect()` does the same check. We can safely remove this `if` block here.

Target:
protected boolean connect() throws IOException { if (super.connect()) { this.sslEngine = sslContext.createSSLEngine(); this.sslEngine.setUseClientMode(true); this.sslSession = sslEngine.getSession(); // Begin handshake sslEngine.beginHandshake(); doHandshake(channel(), sslEngine, ByteBuffer.allocate(sslSession.getPacketBufferSize()), ByteBuffer.allocate(sslSession.getPacketBufferSize())); } return false; }
Source:
public static void checkArgument(boolean expression, String errorMessage, String... args) { if (!expression) { if (args != null && args.length > 0) { START String message = String.format("errorMessage", args); END throw new IllegalArgumentException(message); } else { throw new IllegalArgumentException(errorMessage); } } } `errorMessage` shouldn't be quoted.

Target:
public static void checkArgument(boolean expression, String errorMessage, String... args) { if (!expression) { if (args != null && args.length > 0) { String message = String.format(errorMessage, args); throw new IllegalArgumentException(message); } else { throw new IllegalArgumentException(errorMessage); } } }
Source:
public String toString() { return "BasicDistributionSummary{config=" + config + ", totalAmount=" + totalAmount + ", count=" + count + ", max=" + max + ", min=" + min + START ", monitors=" + monitors + END '}'; } Was it intentional to add the monitors list to the toString output? That seems redundant with the count, min, and max fields already there.

Target:
public String toString() { return "BasicDistributionSummary{config=" + config + ", totalAmount=" + totalAmount + ", count=" + count + ", max=" + max + ", min=" + min + '}'; }
Source:
public int hashCode() { int result = config.hashCode(); result = 31 * result + totalAmount.hashCode(); result = 31 * result + count.hashCode(); result = 31 * result + max.hashCode(); result = 31 * result + min.hashCode(); START result = 31 * result + monitors.hashCode(); END return result; } The monitors list was added to the hash code here, but not for BasicTimer. I don't think it will change the result in this case, but any reason it isn't consistent for both?

Target:
public int hashCode() { int result = config.hashCode(); result = 31 * result + totalAmount.hashCode(); result = 31 * result + count.hashCode(); result = 31 * result + max.hashCode(); result = 31 * result + min.hashCode(); return result; }
Source:
public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || !(obj instanceof MaxGauge)) { return false; } MaxGauge m = (MaxGauge) obj; return config.equals(m.getConfig()); START } END What is the use-case for equality of the gauge being based on the config only? Would it make more sense to just use the default equals/hashCode for the gauge and expect equality checks to use the config directly?

Target:
public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null || !(obj instanceof MaxGauge)) { return false; } MaxGauge m = (MaxGauge) obj; return config.equals(m.getConfig()) && getValue(0).equals(m.getValue(0)); }
Source:
public <T> String display(Type targetType, T value) throws HTMLDisplayerException { START if (targetType == null && value != null) { END return getHTMLDisplayer(value.getClass()).display(value); } else { return getHTMLDisplayer(targetType).display(value); } } Just a detail: I would have probably created a private (maybe it make sense even in public not sure) getHTMLDisplayer(Type targetType, Object value) to put this logic in a common method.

Target:
public <T> String display(Type targetType, T value) throws HTMLDisplayerException { Type type = getType(targetType, value); return getHTMLDisplayer(type).display(type, value); }
Source:
public boolean isDeprecated() { START return deprecated; END } missing `this`

Target:
public boolean isDeprecated() { return this.deprecated; }
Source:
public DatabaseJoinData() { super(); START notfound = null; END } Why are we keeping `notfound = null`? Won't it be `null` without this?

Target:
public DatabaseJoinData() { super(); }
Source:
START public void setMaxSize( int maxSize ) { END this.maxSize = maxSize; } Could this method, and setPurgeTimeout, be package-private? Still testable, and reduces risk of misuse.

Target:
void setMaxSize( int maxSize ) { this.maxSize = maxSize; }
Source:
private boolean safeWaitForCompletion( Future future, long timeoutSeconds ) { try { future.get( timeoutSeconds, TimeUnit.SECONDS ); } catch ( TimeoutException te ) { START System.out.println( "Thread timed out." ); END return false; } catch ( InterruptedException | ExecutionException e ) { fail(); } return true; } Replace sysout. Maybe just fail(message)?

Target:
private boolean safeWaitForCompletion( Future future, long timeoutSeconds ) { try { future.get( timeoutSeconds, TimeUnit.SECONDS ); } catch ( TimeoutException te ) { return false; } catch ( InterruptedException | ExecutionException e ) { fail(); } return true; }
Source:
public void init(FMLInitializationEvent evt) { try { Natura.logger.debug("[MFR] Registering Natura trees/plants/crops with MFR Farming Registry."); MRFRegistering.registerWithMFR(); } catch (Throwable t) { START Natura.logger.debug("Something went wrong in Natura plugin MineFactoryReloaded.", t); END } } this should stay in warn

Target:
public void init(FMLInitializationEvent evt) { try { Natura.logger.debug("[MFR] Registering Natura trees/plants/crops with MFR Farming Registry."); MRFRegistering.registerWithMFR(); } catch (Throwable t) { Natura.logger.warn("Something went wrong in Natura plugin MineFactoryReloaded.", t); } }
Source:
START private static boolean isEjbModule(IModule module) { END IModuleType type = module.getModuleType(); return IWTPConstants.FACET_EJB.equals(type.getId()); //$NON-NLS-1$ } After some thoughs, shouldn't we check that the module is a Web module instead of excluding EJB modules ?

Target:
private static boolean isEjbModule(IModule module) { IModuleType type = module.getModuleType(); if (type == null) { return false; } return IWTPConstants.FACET_EJB.equals(type.getId()); //$NON-NLS-1$ }
Source:
public int getUnhealthy() { START return unhealthy ? 1 : 0; END } It's sort of weird that the getter is an int but the setter is a boolean. Can we just make everything an int, including the underlying variable? Also, did you consider making this an error count instead of just 1/0 since that would provide additional information?

Target:
public int getUnhealthy() { return unhealthy; }
Source:
private static int findFreePort() throws IOException { START try (ServerSocket socket = new ServerSocket(0);) { END return socket.getLocalPort(); } } Unnecessary semicolon

Target:
private static int findFreePort() throws IOException { try (ServerSocket socket = new ServerSocket(0)) { return socket.getLocalPort(); } }
Source:
public void testKerberosAuthenticatorExceptionInExecute() throws Exception { String principal = flumePrincipal; String keytab = flumeKeytab.getAbsolutePath(); // Clear the previous statically stored logged in credentials START FlumeAuthenticationUtil.clearCredentials(); END FlumeAuthenticator authenticator = FlumeAuthenticationUtil.getAuthenticator(principal, keytab); assertTrue(authenticator instanceof KerberosAuthenticator); authenticator.execute(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception { throw new IOException(); } }); } I think this cleanup should be part of tearDown. (Here and in other tests as well.)

Target:
public void testKerberosAuthenticatorExceptionInExecute() throws Exception { String principal = flumePrincipal; String keytab = flumeKeytab.getAbsolutePath(); FlumeAuthenticator authenticator = FlumeAuthenticationUtil.getAuthenticator(principal, keytab); assertTrue(authenticator instanceof KerberosAuthenticator); authenticator.execute(new PrivilegedExceptionAction<Object>() { @Override public Object run() throws Exception { throw new IOException(); } }); }
Source:
private FlumeEventAvroEventDeserializer(Context context, ResettableInputStream ris) { this.ris = ris; START schemaType = AvroSchemaType.valueOf( END context.getString(CONFIG_SCHEMA_TYPE_KEY, AvroSchemaType.HASH.toString()).toUpperCase(Locale.ENGLISH)); if (schemaType == AvroSchemaType.LITERAL) { logger.warn(CONFIG_SCHEMA_TYPE_KEY + " set to " + AvroSchemaType.LITERAL.toString() + ", so storing full Avro " + "schema in the header of each event, which may be inefficient. " + "Consider using the hash of the schema " + "instead of the literal schema."); } } This (line 48-57) is a copy-paste from `AvroEventDeserializer`. Could you please simplify it by pushing it (from here and from the `AvroEventDeserializer`) up to the `AbstractAvroEventDeserializer`? If the logic is moved to the parent class then its `schemaType` field can be changed to `private final`.

Target:
private FlumeEventAvroEventDeserializer(Context context, ResettableInputStream ris) { super(context, ris); }
Source:
public LatestLinkBuilder(File dir) throws IOException { System.out.println(String.format("Writing plugin symlinks and redirects to dir: %s", dir)); index = new IndexHtmlBuilder(dir,"Permalinks to latest files"); START htaccess = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(dir,".htaccess")), "UTF-8")); END htaccess.println("# GENERATED. DO NOT MODIFY."); // Redirect directive doesn't let us write redirect rules relative to the directory .htaccess exists, // so we are back to mod_rewrite htaccess.println("RewriteEngine on"); } Hasn't the second parameter `true` (for `autoFlush`) been forgotten in the go?

Target:
public LatestLinkBuilder(File dir) throws IOException { System.out.println(String.format("Writing plugin symlinks and redirects to dir: %s", dir)); index = new IndexHtmlBuilder(dir,"Permalinks to latest files"); htaccess = new PrintWriter(new OutputStreamWriter(new FileOutputStream(new File(dir,".htaccess"), true), "UTF-8")); htaccess.println("# GENERATED. DO NOT MODIFY."); // Redirect directive doesn't let us write redirect rules relative to the directory .htaccess exists, // so we are back to mod_rewrite htaccess.println("RewriteEngine on"); }
Source:
public String getString(String key, String defaultValue) { Object value = getRawProperty(key); if (value == null) { return notFound(key, defaultValue != null ? interpolator.create(getLookup()).resolve(defaultValue) : null); } if (value instanceof String) { START return resolve(value.toString()); END } else { return value.toString(); } } casting would be faster...

Target:
public String getString(String key, String defaultValue) { Object value = getRawProperty(key); if (value == null) { return notFound(key, defaultValue != null ? interpolator.create(getLookup()).resolve(defaultValue) : null); } if (value instanceof String) { return resolve((String)value); } else { return value.toString(); } }
Source:
public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; START throw new ProvisionException("Only one ArchaiusModule may be installed"); END } Having equals throw to enforce a singleton module policy seems unintuitive. Does this really add much over the normal duplicate binding for Config error?

Target:
public boolean equals(Object obj) { return this == obj; }
Source:
public DynamicBooleanProperty(String propName, boolean defaultValue) { super(propName, Boolean.valueOf(defaultValue)); // Set the initial value of the cached primitive value. this.primitiveValue = chooseValue(); // Add a callback to update the cached primitive value when the property is changed. START this.prop.addCallback(new Runnable() { END @Override public void run() { primitiveValue = chooseValue(); } }); } Rather than adding this callback here, you could just override either of the propertyChanged methods and then update the PropertyWrapper class to not add this class to the SUBCLASSES_WITH_NO_CALLBACK map. This would be cleaner and probably result in less overall code.

Target:
public DynamicBooleanProperty(String propName, boolean defaultValue) { super(propName, Boolean.valueOf(defaultValue)); }
Source:
public String getValue(ContextKey key) { if (delegate == null) { START LOG.debug("Configuration not yet initialized. Returning 'null' for " + key); END return null; } return delegate.getValue(key); } Just curious, why does this class use logging but StaticAbstractConfiguration is being changed to System.err?

Target:
public String getValue(ContextKey key) { if (delegate == null) { System.out.println("Configuration not yet initialized. Returning 'null' for " + key); return null; } return delegate.getValue(key); }
Source:
public void close() { try { Closeables.close(pathChildrenCache, true); } catch (IOException exc) { START logger.warn("IOException should not have been thrown.", exc); END } } If you want to maintain the same behavior as before you should probably log this as ERROR since guava is logging it as SEVERE.

Target:
public void close() { try { Closeables.close(pathChildrenCache, true); } catch (IOException exc) { logger.error("IOException should not have been thrown.", exc); } }
Source:
protected void propertyChanged() { START super.propertyChanged(); END // derived.set(this.derive(this.get())); deriveAndSet(); } what if people override this method and forget to call super.propertyChanged(). Then the derived value will no longer be available. You might want to look at DynamicSetProperty and see if you can use the same trick to avoid that from happening.

Target:
protected void propertyChanged()
Source:
void propertyChangedInternal() { propertyChanged(); START deriveAndSet(); END } I think deriveAndSet() should be called first. Then in propertyChanged(), users can call get to get() the updated derived value.

Target:
void propertyChangedInternal() { deriveAndSet(); propertyChanged(); }
Source:
private boolean isLogDataValid(long address, ILogData logData, boolean throwException) { if (logData == null || logData.getType() == DataType.EMPTY) { throw new RuntimeException("Unexpected return of empty data at address " + address + " on read"); } if (logData.isTrimmed()) { if (throwException) { START throw new TrimmedException(Collections.singletonList(address)); END } return false; } return true; } Cleaner to push down to the constructor.

Target:
private boolean isLogDataValid(long address, ILogData logData, boolean throwException) { if (logData == null || logData.getType() == DataType.EMPTY) { throw new RuntimeException("Unexpected return of empty data at address " + address + " on read"); } if (logData.isTrimmed()) { if (throwException) { throw new TrimmedException(address); } return false; } return true; }
Source:
public void start() { // The DISCOVERY_INIT_TOPOLOGY should be the first event to be processed for both ACTIVE and STANDBY sites. this.eventQueue.add(new DiscoveryServiceEvent(DISCOVER_INIT_TOPOLOGY)); this.executorService.submit(this::run); START startDiscovery(); END } wasn't the purpose of DISCOVER_INIT_TOPOLOGY to call startDiscovery from there? DISCOVER_INIT_TOPOLOGY is not a supported event (processEvent)

Target:
public void start() { // The DISCOVERY_INIT_TOPOLOGY should be the first event to be processed for both ACTIVE and STANDBY sites. this.eventQueue.add(new DiscoveryServiceEvent(DISCOVER_INIT_TOPOLOGY)); this.executorService.submit(this::run); }
Source:
LogReplicationEntry generateMessageWithOpaqueEntryList(List<OpaqueEntry> opaqueEntryList, UUID logEntryRequestId) { // Set the last timestamp as the max timestamp currentMsgTs = opaqueEntryList.get(opaqueEntryList.size() - 1).getVersion(); START END LogReplicationEntry txMessage = new LogReplicationEntry(MSG_TYPE, topologyConfigId, logEntryRequestId, currentMsgTs, preMsgTs, globalBaseSnapshot, sequence, opaqueEntryList); preMsgTs = currentMsgTs; sequence++; log.trace("Generate a log entry message with transactions ", txMessage.getMetadata(), opaqueEntryList.size()); return txMessage; } nit - extra newline

Target:
private LogReplicationEntry generateMessageWithOpaqueEntryList(List<OpaqueEntry> opaqueEntryList, UUID logEntryRequestId) { // Set the last timestamp as the max timestamp currentMsgTs = opaqueEntryList.get(opaqueEntryList.size() - 1).getVersion(); LogReplicationEntry txMessage = new LogReplicationEntry(MSG_TYPE, topologyConfigId, logEntryRequestId, currentMsgTs, preMsgTs, globalBaseSnapshot, sequence, opaqueEntryList); preMsgTs = currentMsgTs; sequence++; log.trace("Generate a log entry message with transactions ", txMessage.getMetadata(), opaqueEntryList.size()); return txMessage; }
Source:
START boolean shouldProcess(OpaqueEntry entry) { END Set<UUID> tmpUUIDs = entry.getEntries().keySet(); //If the entry's stream set is a subset of interested streams, it is the entry we should process if (streamUUIDs.containsAll(tmpUUIDs)) { return true; } //If the entry's stream set has no overlap with the interested streams, it should be skipped. tmpUUIDs.retainAll(streamUUIDs); if (tmpUUIDs.isEmpty()) { return false; } // If the entry's stream set contains both interested streams and other streams, it is not // the expected behavior log.error("There are noisy streams in the entry, expected streams set {}", entry.getEntries().keySet(), streamUUIDs); hasNoiseData = true; return false; } private

Target:
private boolean shouldProcess(OpaqueEntry entry) { Set<UUID> tmpUUIDs = entry.getEntries().keySet(); //If the entry's stream set is a subset of interested streams, it is the entry we should process if (streamUUIDs.containsAll(tmpUUIDs)) { return true; } //If the entry's stream set has no overlap with the interested streams, it should be skipped. tmpUUIDs.retainAll(streamUUIDs); if (tmpUUIDs.isEmpty()) { return false; } // If the entry's stream set contains both interested streams and other streams, it is not // the expected behavior log.error("There are noisy streams in the entry, expected streams set {}", entry.getEntries().keySet(), streamUUIDs); hasNoiseData = true; return false; }
Source:
public LogReplicationMetadata.ReplicationStatusVal queryReplicationStatus() { return corfuReplicationDiscoveryService.queryReplicationStatus( START topologyConfig.getClusters(0).getId()); END } It's not guaranteed to always place the local cluster in the 0th index position right? Am I mistaken?

Target:
public Map<String, LogReplicationMetadata.ReplicationStatusVal> queryReplicationStatus() { return corfuReplicationDiscoveryService.queryReplicationStatus(); }
Source:
START long query(CorfuStoreMetadata.Timestamp timestamp, LogReplicationMetadataType key) { END long val = -1; String str = queryString(timestamp, key); if (str != null) { val = Long.parseLong(str); } return val; } Lets add public/private accessor. It should be private right. as we have wrappers for each metadata type.

Target:
public long query(CorfuStoreMetadata.Timestamp timestamp, LogReplicationMetadataType key) { long val = -1; String str = queryString(timestamp, key); if (str != null) { val = Long.parseLong(str); } return val; }
Source:
START long getLastProcessed() { END return logReplicationMetadataManager.getLastProcessedLogTimestamp(null); } private, public?

Target:
public long getLastProcessed() { return logReplicationMetadataManager.getLastProcessedLogTimestamp(null); }
Source:
START TxBuilder getTxBuilder() { END return corfuStore.tx(namespace); } private

Target:
public TxBuilder getTxBuilder() { return corfuStore.tx(namespace); }
Source:
public CompletableFuture<LogReplicationEntry> sendWithBuffering(LogReplicationEntry message) { //todo need to be removed. message.getMetadata().setSnapshotSyncSeqNum(maxAckedMsgSeqNum++); pendingMessages.append(message); START log.info("Send messsage {}", message.getMetadata()); END CompletableFuture<LogReplicationEntry> cf = dataSender.send(message); addCFToAcked(message, cf); return cf; } I think we can remove this or downgrade to trace as we already log right before sending.

Target:
public CompletableFuture<LogReplicationEntry> sendWithBuffering(LogReplicationEntry message) { message.getMetadata().setSnapshotSyncSeqNum(maxAckedMsgSeqNum++); pendingMessages.append(message); log.trace("Send messsage {}", message.getMetadata()); CompletableFuture<LogReplicationEntry> cf = dataSender.send(message); addCFToAcked(message, cf); return cf; }
Source:
private void completeSnapshotApply(LogReplicationEntry inputEntry) { log.debug("Complete of a snapshot apply"); //check if the all the expected message has received rxState = RxState.LOG_ENTRY_SYNC; logReplicationMetadataManager.setSnapshotApplied(inputEntry); START logEntrySinkBufferManager = new LogEntrySinkBufferManager(ackCycleTime, ackCycleCnt, bufferSize, END logReplicationMetadataManager.getLastProcessedLogTimestamp(), this); log.info("Sink manager completed SNAPSHOT transfer for and has transit to state.", inputEntry, rxState); } Do we need a new BufferManager on every snapshotApply? or could we just reset?

Target:
private void completeSnapshotApply(LogReplicationEntry inputEntry) { log.debug("Complete of a snapshot apply"); //check if the all the expected message has received rxState = RxState.LOG_ENTRY_SYNC; logReplicationMetadataManager.setSnapshotApplied(inputEntry); //logEntrySinkBufferManager = new LogEntrySinkBufferManager(ackCycleTime, ackCycleCnt, bufferSize, // logReplicationMetadataManager.getLastProcessedLogTimestamp(), this); log.info("Sink manager completed SNAPSHOT transfer for and has transit to state.", inputEntry, rxState); }
Source:
START private CompletableFuture<CorfuInterClusterReplicationServerNode> startDiscoveryService(ServerContext serverContext) throws InterruptedException { END log.info("Start Discovery Service."); CompletableFuture<CorfuInterClusterReplicationServerNode> discoveryServiceCallback = new CompletableFuture<>(); this.clusterManagerAdapter = buildClusterManagerAdapter(serverContext.getPluginConfigFilePath()); // Start LogReplicationDiscovery Service, responsible for // acquiring lock, retrieving Site Manager Info and processing this info // so this node is initialized as Source (sender) or Sink (receiver) replicationDiscoveryService = new CorfuReplicationDiscoveryService(serverContext, clusterManagerAdapter, discoveryServiceCallback); return discoveryServiceCallback; } throws InterruptedException is not used.

Target:
private CompletableFuture<CorfuInterClusterReplicationServerNode> startDiscoveryService(ServerContext serverContext) { log.info("Start Discovery Service."); CompletableFuture<CorfuInterClusterReplicationServerNode> discoveryServiceCallback = new CompletableFuture<>(); this.clusterManagerAdapter = buildClusterManagerAdapter(serverContext.getPluginConfigFilePath()); // Start LogReplicationDiscovery Service, responsible for // acquiring lock, retrieving Site Manager Info and processing this info // so this node is initialized as Source (sender) or Sink (receiver) replicationDiscoveryService = new CorfuReplicationDiscoveryService(serverContext, clusterManagerAdapter, discoveryServiceCallback); replicationDiscoveryService.start(); return discoveryServiceCallback; }
Source:
public CompletableFuture<LogReplicationQueryMetadataResponse> sendQueryMetadataRequest() { START return null; END } I think we can return new CompletableFuture<>(); Otherwise, we hit NPE...

Target:
public CompletableFuture<LogReplicationQueryMetadataResponse> sendQueryMetadataRequest() { return new CompletableFuture<>(); }
Source:
public void cleanShutdown() { log.info("CleanShutdown: Starting Cleanup."); shutdownServer = true; START activeServer.close(); replicationDiscoveryService.shutdown(); clusterManagerAdapter.shutdown(); END } Need to check those variables are initialized or not to avoid NPE.

Target:
public void cleanShutdown() { log.info("CleanShutdown: Starting Cleanup."); shutdownServer = true; activeServer.close(); if (replicationDiscoveryService != null) { replicationDiscoveryService.shutdown(); } }
Source:
public TopologyDescriptor(TopologyConfigurationMsg topologyMessage) { this.topologyConfigId = topologyMessage.getTopologyConfigID(); this.certs = topologyMessage.getCerts(); standbyClusters = new HashMap<>(); for (ClusterConfigurationMsg clusterConfig : topologyMessage.getClustersList()) { ClusterDescriptor siteInfo = new ClusterDescriptor(clusterConfig); START if (clusterConfig.getRole() == ClusterRole.ACTIVE) { activeCluster = siteInfo; END } else if (clusterConfig.getRole() == ClusterRole.STANDBY) { addStandbySite(siteInfo); } } } Do we assume that topologyMessage from the cluster manager is always valid? I mean it always has only one active cluster. If not, we can throw an exception or at least have some debug logs.

Target:
public TopologyDescriptor(TopologyConfigurationMsg topologyMessage) { this.topologyConfigId = topologyMessage.getTopologyConfigID(); this.standbyClusters = new HashMap<>(); for (ClusterConfigurationMsg clusterConfig : topologyMessage.getClustersList()) { ClusterDescriptor cluster = new ClusterDescriptor(clusterConfig); if (clusterConfig.getRole() == ClusterRole.ACTIVE) { activeCluster = cluster; } else if (clusterConfig.getRole() == ClusterRole.STANDBY) { addStandbySite(cluster); } } }
Source:
private Layout updateLayoutAndGet() { return getCorfuRuntime() .invalidateLayout() .thenApply(serverContext::saveManagementLayout) .join(); START } END what's the motivation, why you need to update the layout? FD is in charge of doing it, not sure if it safe to update the layout here

Target:
private Layout updateLayoutAndGet() { return getCorfuRuntime() .invalidateLayout() .join(); }
Source:
public int getSizeEstimate() { byte[] tempData = data; if (tempData != null) { START return tempData.length + metadataMap.size() + DataType.values().length; END } else if (lastKnownSize != NOT_KNOWN) { return lastKnownSize; } log.warn("getSizeEstimate: LogData size estimate is defaulting to 1," + " this might cause leaks in the cache!"); return 1; } metadataMap.size() is the number of entries, not the size.

Target:
public int getSizeEstimate() { if (lastKnownSize != NOT_KNOWN) { return lastKnownSize; } return 1; }
Source:
public void AddNodeDuringInvalidateLayoutTest() { workflow(wf -> { wf.setupDocker(fixture -> { START fixture.getCluster().numNodes(3); END }); wf.deploy(); try { addNodeDuringInvalidateLayout(wf); } catch (Exception e) { Assertions.fail("Test failed: " + e); } } ); } it is a 3 node cluster by default, don't need to specify it

Target:
public void AddNodeDuringInvalidateLayoutTest() { workflow(wf -> { wf.deploy(); try { addNodeDuringInvalidateLayout(wf); } catch (Exception e) { Assertions.fail("Test failed: " + e); } } ); }
Source:
public void channelActive(final ChannelHandlerContext ctx) throws Exception { START log.trace("channelActive: Incoming connection established from: Start Read Timeout.", END ctx.channel().remoteAddress()); ctx.pipeline().addBefore(ctx.name(), READ_TIMEOUT_HANDLER, new ReadTimeoutHandler(this.timeoutInSeconds)); } This log statement I believe is also relevant when we're not sure if the connection from one client to a server is established. It is useful, I think we should leave it as INFO or DEBUG.

Target:
public void channelActive(final ChannelHandlerContext ctx) throws Exception { log.info("channelActive: Incoming connection established from: Start Read Timeout.", ctx.channel().remoteAddress()); ctx.pipeline().addBefore(ctx.name(), READ_TIMEOUT_HANDLER, new ReadTimeoutHandler(this.timeoutInSeconds)); }
Source:
public List<SMREntry> current() { ILogData data = streamView.current(); END START if (data != null && data.isHole()) { throw new HoleInStreamException(); } if (data == null || data.getType() != DataType.DATA || !(data.getPayload(runtime) instanceof ISMRConsumable)) { return Collections.emptyList(); } return ((ISMRConsumable) data.getPayload(runtime)).getSMRUpdates(streamView.getId()); } Rather than signaling this condition via an exception, maybe a better approach would be to have a globally unique instance that signals a hole.

Target:
public List<SMREntry> current() { ILogData data = streamView.current(); if (data == null || data.getType() != DataType.DATA || !(data.getPayload(runtime) instanceof ISMRConsumable)) { return Collections.emptyList(); } return ((ISMRConsumable) data.getPayload(runtime)).getSMRUpdates(streamView.getId()); }
Source:
private void updatePointer(final ILogData data) { // Update the global pointer, if it is non-checkpoint data. START if ((data.isData() && !data.hasCheckpointMetadata()) || (data.isHole() && (data.getBackpointerMap().isEmpty() || data.containsStream(getCurrentContext().id))) ) { END // Note: here we only set the global pointer and do not validate its position with respect to the trim mark, // as the pointer is expected to be moving step by step (for instance when syncing a stream up to maxGlobal) // The validation is deferred to these methods which call it in advance based on the expected final position // of the pointer. getCurrentContext().setGlobalPointer(data.getGlobalAddress()); } } I think we probably don't need to check containsStream here because when we call this method, logdata that does not belong to this stream should already be filtered out, could you double check?

Target:
private void updatePointer(final ILogData data) { // Update the global pointer, if it is non-checkpoint data. if ((data.isData() || data.isHole()) && !data.hasCheckpointMetadata()) { // Note: here we only set the global pointer and do not validate its position with respect to the trim mark, // as the pointer is expected to be moving step by step (for instance when syncing a stream up to maxGlobal) // The validation is deferred to these methods which call it in advance based on the expected final position // of the pointer. getCurrentContext().setGlobalPointer(data.getGlobalAddress()); } }
Source:
public void setServer(AbstractServer server) { router.reset(); START router.setIgnoreMessageValidation(true); END router.addServer(server); } Why ignore the epoch/cluster id check?

Target:
public void setServer(AbstractServer server) { router.reset(); router.addServer(server); }
Source:
default void sendWrongEpochMessage(CorfuMsg msg, ChannelHandlerContext ctx) { sendResponse(ctx, msg, new CorfuPayloadMsg<>(CorfuMsgType.WRONG_EPOCH, getServerEpoch())); START log.warn("Incoming message with wrong epoch, got {}, expected {}, message was: {}", END msg.getEpoch(), getServerEpoch(), msg); } This log line and the the ones in the following methods are trace level in master, why we made it warning level?

Target:
default void sendWrongEpochMessage(CorfuMsg msg, ChannelHandlerContext ctx) { sendResponse(ctx, msg, new CorfuPayloadMsg<>(CorfuMsgType.WRONG_EPOCH, getServerEpoch())); log.trace("Incoming message with wrong epoch, got {}, expected {}, message was: {}", msg.getEpoch(), getServerEpoch(), msg); }
Source:
public Stream<Entry<K, V>> entryStream() { final ReadOptions readOptions = new ReadOptions(); // If ReadOptions.snapshot is given, the iterator will return data as of the snapshot. // If it is nullptr, the iterator will read from an implicit snapshot as of the time the END // iterator is created. The implicit snapshot is preserved by pinning resource. START readOptions.setSnapshot(null); final RocksDbEntryIterator entryIterator = new RocksDbEntryIterator(rocksDb.newIterator(readOptions), serializer); // It's necessary to use the RocksDbEntryIterator as try-with resource to ensure // the iterator is close, otherwise underlying RocksDB resources can leak Stream<Entry<K, V>> resStream = Streams.stream(entryIterator); resStream.onClose(entryIterator::close); return resStream; } According to this: https://github.com/facebook/rocksdb/blob/master/java/src/main/java/org/rocksdb/ReadOptions.java#L11 I think we need to call `readOptions.close()` before returning to prevent memory leak. (dispose is deprecated, so using close())

Target:
public Stream<Entry<K, V>> entryStream() { final RocksDbEntryIterator entryIterator = new RocksDbEntryIterator(rocksDb, serializer); Stream<Entry<K, V>> resStream = Streams.stream(entryIterator); resStream.onClose(entryIterator::close); return resStream; }
Source:
START long firstAddress() { END if (cacheEntries.peek() == null) return Address.NOT_FOUND;; return cacheEntries.peek().txVersion; } can we add an access specifier here?

Target:
public long firstAddress() { if (cacheEntries.isEmpty()) { return Address.NOT_FOUND; } return cacheEntries.peek().txVersion; }
Source:
private long firstAddress() { START if (cacheEntries.peek() == null) END return Address.NOT_FOUND; return cacheEntries.peek().txVersion; } Please replace by `if(!cacheEntries.isEmpty())`

Target:
public long firstAddress() { if (cacheEntries.isEmpty()) { return Address.NOT_FOUND; } return cacheEntries.peek().txVersion; }
Source:
public long firstAddress() { if (cacheEntries.isEmpty()) START return Address.NOT_FOUND; END return cacheEntries.peek().txVersion; } Please use curly brackets: https://stackoverflow.com/a/15786982

Target:
public long firstAddress() { if (cacheEntries.isEmpty()) { return Address.NOT_FOUND; } return cacheEntries.peek().txVersion; }
Source:
public static Config parse(Map<String, Object> opts) { int cacheSize = (int)(opts.containsKey("--sequencer-cache-size") ? START Long.parseLong((String)opts.get("--sequencer-cache-size")) : DEFAULT_CACHE_SIZE); END return Config.builder() .cacheSize(cacheSize) .build(); } why parse into a long and then typecast to an integer? Can we use Integer.parseInt() instead?

Target:
public static Config parse(Map<String, Object> opts) { int cacheSize = (int)(opts.containsKey("--sequencer-cache-size") ? Integer.parseInt((String)opts.get("--sequencer-cache-size")) : DEFAULT_CACHE_SIZE); return Config.builder() .cacheSize(cacheSize) .build(); }
Source:
private CorfuRuntime createDefaultRuntimeUsingFollowBackpointers() { CorfuRuntime runtime = createRuntime(DEFAULT_ENDPOINT); START runtime.getParameters().setFollowBackpointersEnabled(false) END .setCacheDisabled(false); return runtime; } this one is intended to use the follow backpointers implementation, so it should be set to true.

Target:
private CorfuRuntime createDefaultRuntimeUsingFollowBackpointers() { CorfuRuntime runtime = createRuntime(DEFAULT_ENDPOINT); runtime.getParameters().setFollowBackpointersEnabled(true) .setCacheDisabled(false); return runtime; }
Source:
public boolean getBindToAllInterfaces() { START if (getNetworkInterface() != null || getHostAddress() != null) { END return false; } else { return true; } } ![Codacy](https://app.codacy.com/assets/images/favicon.png) Issue found: [Avoid unnecessary if..then..else statements when returning booleans](https://app.codacy.com/app/CorfuDB/CorfuDB/pullRequest?prid=4662120)

Target:
public boolean getBindToAllInterfaces() { // Bind to all interfaces only if a network interface or host address // hasn't been specified return !(getNetworkInterface() != null || getHostAddress() != null); }
Source:
START void processRequest(CorfuMsg msg, ChannelHandlerContext ctx, IServerRouter r) { END getHandler().handle(msg, ctx, r); } ![Codacy](https://app.codacy.com/assets/images/favicon.png) Issue found: [Use explicit scoping instead of the default package private level](https://app.codacy.com/app/annymartinez.m/CorfuDB/pullRequest?prid=4651438)

Target:
protected void processRequest(CorfuMsg msg, ChannelHandlerContext ctx, IServerRouter r) { getHandler().handle(msg, ctx, r); }
Source:
public void addIndexerToCorfuTableStream(String streamName, IndexRegistry indexRegistry) { UUID streamId = CorfuRuntime.getStreamID(streamName); SMRObject.Builder ob = SMRObject.builder() .runtime(runtime) .setType(CorfuTable.class) START .setArguments(indexRegistry) END .setStreamID(streamId); addCustomTypeStream(streamId, ob); } use `type(CorfuTable)` instead of setType

Target:
public void addIndexerToCorfuTableStream(String streamName, IndexRegistry indexRegistry) { UUID streamId = CorfuRuntime.getStreamID(streamName); Builder ob = SMRObject.builder() .runtime(runtime) .setType(CorfuTable.class) .setArguments(indexRegistry) .setStreamID(streamId); addCustomTypeStream(streamId, ob); }
Source:
public void stop() { START if (!isRunning.get()) { END log.info("Garbage informer had been stopped."); return; } gcScheduler.shutdownNow(); log.info("Garbage informer is stopped."); isRunning.set(false); } I think it's better to use compareAndSet just as what you did in start()

Target:
public synchronized void stop() { if (!isRunning) { log.info("Garbage informer had been stopped."); return; } gcScheduler.shutdownNow(); log.info("Garbage informer is stopped."); isRunning = false; }
Source:
START public DetectorTask restoreRedundancy(Layout layout){ END return restoreRedundancyAndMergeSegments(layout); } what's the point for the wrapper method?

Target:
public void restoreRedundancy(Layout layout) { restoreRedundancyAndMergeSegments(layout); }
Source:
public void impl(@Nonnull CorfuRuntime runtime) throws Exception { START runtime.invalidateLayout(); END Layout layout = new Layout(runtime.getLayoutView().getLayout()); runtime.getLayoutManagementView().removeNode(layout, request.getEndpoint()); } The orchestrator will pass down a connected client (i.e. it will have a layout ready) when it creates a workflow, why is this needed?

Target:
public void impl(@Nonnull CorfuRuntime runtime) throws Exception { Layout layout = new Layout(runtime.getLayoutView().getLayout()); runtime.getLayoutManagementView().removeNode(layout, request.getEndpoint()); }
Source:
public static boolean canMergeSegments(Layout layout) { if (layout.getSegments().size() == 1) { return false; } else { START int firstSegmentIndex = 0; int secondSegmentIndex = 1; END return Sets.difference( layout.getSegments().get(secondSegmentIndex).getAllLogServers(), layout.getSegments().get(firstSegmentIndex).getAllLogServers()).isEmpty(); } } final int

Target:
public static boolean canMergeSegments(Layout layout) { if (layout.getSegments().size() == 1) { return false; } else { final int firstSegmentIndex = 0; final int secondSegmentIndex = 1; return Sets.difference( layout.getSegments().get(secondSegmentIndex).getAllLogServers(), layout.getSegments().get(firstSegmentIndex).getAllLogServers()).isEmpty(); } }
Source:
public StreamPerformanceTest() { START metricRegistry = CorfuRuntime.getDefaultMetrics(); END producerTimer = metricRegistry.timer(METRIC_PREFIX + "stream-producer"); consumerTimer = metricRegistry.timer(METRIC_PREFIX + "stream-consumer"); } ![Codacy](https://app.codacy.com/assets/images/favicon.png) Issue found: [Possible unsafe assignment to a non-final static field in a constructor.](https://app.codacy.com/app/annymartinez.m/CorfuDB/pullRequest?prid=4651454)

Target:
public StreamPerformanceTest() { producerTimer = metricRegistry.timer(METRIC_PREFIX + "stream-producer"); consumerTimer = metricRegistry.timer(METRIC_PREFIX + "stream-consumer"); }
Source:
START String getKey() { END return keySet.get(random.nextInt(keySet.size())); } ![Codacy](https://app.codacy.com/assets/images/favicon.png) Issue found: [Use explicit scoping instead of the default package private level](https://app.codacy.com/app/annymartinez.m/CorfuDB/pullRequest?prid=4651454)

Target:
protected String getKey() { return keySet.get(random.nextInt(keySet.size())); }
Source:
START String generateValue() { END return RandomStringUtils.random(valueSize, true, true); } ![Codacy](https://app.codacy.com/assets/images/favicon.png) Issue found: [Use explicit scoping instead of the default package private level](https://app.codacy.com/app/annymartinez.m/CorfuDB/pullRequest?prid=4651454)

Target:
protected String generateValue() { return RandomStringUtils.random(valueSize, true, true); }
Source:
public Entry<K, V> next() { START checkInvariants(); END if (hasNext()) { current = next; next = null; return current; } else { throw new NoSuchElementException(); } } You can remove this since `hasNext` will call `checkInvariants`

Target:
public Entry<K, V> next() { if (hasNext()) { current = next; next = null; return current; } else { throw new NoSuchElementException(); } }
Source:
private static CorfuRuntime getRuntimeAndConnect(String configurationString) { return CorfuRuntime.fromParameters(CorfuRuntime.CorfuRuntimeParameters.builder() .useFastLoader(false) .cacheDisabled(true) .tlsEnabled(true) START .keyStore("/config/cluster-manager/corfu/private/keystore.jks") .ksPasswordFile("/config/cluster-manager/corfu/private/keystore.password") .trustStore("/config/cluster-manager/corfu/public/truststore.jks") .tsPasswordFile("/config/cluster-manager/corfu/public/truststore.password") END .build()) .parseConfigurationString(configurationString) .connect(); } Why the files are using these paths instead of a local file in the resource folder?

Target:
private static CorfuRuntime getRuntimeAndConnect(String configurationString) { return CorfuRuntime.fromParameters(CorfuRuntime.CorfuRuntimeParameters.builder() .useFastLoader(false) .cacheDisabled(true) .tlsEnabled(false) .build()) .parseConfigurationString(configurationString) .connect(); }
Source:
public static <K, V> TypeToken<CorfuTable<K, V>> getMapType() { return new TypeToken<CorfuTable<K, V>>() { }; END START } Can you please revert this file. It will cause bunch of conflicts with my changes.

Target:
getMapType() { return new TypeToken<CorfuTable<K, V>>() {}; }
Source:
START private void closeSegments(AbstractLogSegment... segments) { END for (AbstractLogSegment segment : segments) { if (segment != null) { segment.close(); } } } isn't it cleaner to keep this function as append?

Target:
private void closeSegments(AbstractLogSegment... segments) { for (AbstractLogSegment segment : segments) { if (segment != null) { segment.close(true); } } }
Source:
public boolean contains(long address) { int retryCount = 0; while (true) { try { StreamLogSegment segment = segmentManager.getStreamLogSegment(address); return segment.contains(address); } catch (ClosedSegmentException e) { log.warn("Segment channel closed by compactor, retry for another time."); // Segment could be closed because of compaction, retry once. if (retryCount++ == CLOSE_SEGMENT_EXCEPTION_RETRY) { throw e; } } } START } END the English here is a bit strange. Can we change to : '..., retry.'

Target:
public boolean contains(long address) { StreamLogSegment segment = segmentManager.getStreamLogSegment(address); return segment.contains(address); }
Source:
void runConsumers() { for (int i = 0; i < numThreads; i++) { workers.execute(() -> { try { long start = System.nanoTime(); Operation op = operationQueue.take(); op.execute(); long latency = System.nanoTime() - start; START System.out.println("nextToken request latency: " + latency); END } catch (Exception e) { log.error("Operation failed with", e); } }); } } Using System.out.println() is forbidden. Use a logger or output utility. [printLine]

Target:
void runConsumers() { for (int i = 0; i < numThreads; i++) { workers.execute(() -> { try { long start = System.nanoTime(); Operation op = operationQueue.take(); op.execute(); long latency = System.nanoTime() - start; log.info("nextToken request latency: " + latency); } catch (Exception e) { log.error("Operation failed with", e); } }); } }
Source:
CorfuRecord<V, M> getRecord(@Nonnull final String tableName, @Nonnull final K key) { START return get(tableName, null, key); END } Should this be `return getRecord(tableName, null, key)`?

Target:
CorfuRecord<V, M> getRecord(@Nonnull final String tableName, @Nonnull final K key) { return getRecord(tableName, null, key); }
Source:
private String getTypeUrl(Descriptor descriptor) { START return "type.googleapis.com/" + descriptor.getFullName(); END } Declare as final static..

Target:
private final static String getTypeUrl(Descriptor descriptor) { return "type.googleapis.com/" + descriptor.getFullName(); }
Source:
private void cleanUpForRetry() { runtime.getAddressSpaceView().invalidateClientCache(); runtime.getObjectsView().getObjectCache().clear(); runtime.getStreamsView().getStreamCache().clear(); // Re ask for the Head, if it changes while we were trying. findAndSetLogHead(); START findAndSetLogTail(); END nextRead = logHead; resetAddressProcessed(); log.info("retry fastobject loader head " + logHead + " tail " + logTail); } Should we add a validation that the head and tail chosen are chosen on the same epoch? Probably this is already taken care of by the inherent order of setting the head before the tail, but I think it would be safer to put this validation here.

Target:
private void cleanUpForRetry() { runtime.getAddressSpaceView().invalidateClientCache(); runtime.getObjectsView().getObjectCache().clear(); runtime.getStreamsView().getStreamCache().clear(); log.info("retry fastobject loader head " + logHead + " tail " + logTail); }
Source:
private boolean cacheReadRequest(ReadOptions options) { if (runtime.getParameters().isCacheDisabled() && options.isClientCacheable()) { // TODO(Maithem): We can remove this once we have multi-level caches START if (runtime.getParameters().getPriorityLevel() != PriorityLevel.HIGH) { END // Avoid log flooding for background tasks (eg. checkpointer) log.warn("read: Runtime cache is disabled, but read request is requesting to cache"); } } return !runtime.getParameters().isCacheDisabled() && options.isClientCacheable(); } On my old review it was suggested to have this log line, I would vote for just removing it and not making this distinction.

Target:
private boolean cacheReadRequest(ReadOptions options) { return !runtime.getParameters().isCacheDisabled() && options.isClientCacheable(); }
Source:
START public int getGarbageSizeUpTo(long addressUpTo) { END return streamIdToGarbageMap.values().stream() .flatMap(multiSMRGarbage -> multiSMRGarbage.values().stream()) .map(smrRecordGarbage -> smrRecordGarbage.getSmrEntrySize()) .reduce(0, (a, b) -> a + b); } Why `addressUpTo` is not used? For your reference, following codes are what I used to use: ```java public int getGarbageSizeUpTo(long addressUpTo) { return streamIdToGarbageMap.values().stream() .map(multiSmr -> multiSmr.getGarbageSizeUpTo(addressUpTo)) .reduce(0, (a, b) -> a + b); } ```

Target:
public int getGarbageSizeUpTo(long addressUpTo) { return streamIdToGarbageMap.values().stream() .flatMap(multiSMRGarbage -> multiSMRGarbage.values().stream()) .map(smrRecordGarbage -> smrRecordGarbage.getGarbageSizeUpTo(addressUpTo)) .reduce(0, (a, b) -> a + b); }