auphong2707/dl4se-code-review-dataset huggingface dataset:
Source:
SendThread(ClientCnxnSocket clientCnxnSocket) { super(makeThreadName("-SendThread()")); try { changeZkState(States.CONNECTING); } catch (IOException e) { START throw new RuntimeException("Connection setup failed when migrate state to CONNECTING"); END } this.clientCnxnSocket = clientCnxnSocket; setDaemon(true); } Throwing a RuntimeException may lead to unexpected behaviour. So we have a way to throw a checked exception and deal explicitly with this case?

Target:
SendThread(ClientCnxnSocket clientCnxnSocket) throws IOException { super(makeThreadName("-SendThread()")); changeZkState(States.CONNECTING); this.clientCnxnSocket = clientCnxnSocket; setDaemon(true); }
Source:
public boolean truncateLog(long zxid) { try { // close the existing txnLog and snapLog close(); // truncate it FileTxnLog truncLog = new FileTxnLog(dataDir); boolean truncated = truncLog.truncate(zxid); truncLog.close(); // re-open the txnLog and snapLog // I'd rather just close/reopen this object itself, however that // would have a big impact outside ZKDatabase as there are other // objects holding a reference to this object. txnLog = new FileTxnLog(dataDir); snapLog = new FileSnap(snapDir); return truncated; START } catch (IOException e) { END LOG.error("Unable to truncate Txn log", e); return false; } } I'm wondering if IOException is sufficient. If you look at https://github.com/apache/zookeeper/blob/c234848caef0cc920f19befd1b0b14251b17da92/zookeeper-server/src/main/java/org/apache/zookeeper/server/quorum/Follower.java#L128 It's catching an Exception, so basically any exception other than IOException occurring here, could kick us back into the same situation. In fact, even the caller of Follower.followLeader() catches Exception. They're all over QuorumPeer, with "Unexpected exception" log warnings. I think as part of the same fix, these "Unexpected exception" errors should also take down the process. wdyt?

Target:
public boolean truncateLog(long zxid) { try { // close the existing txnLog and snapLog close(); // truncate it try (FileTxnLog truncLog = new FileTxnLog(dataDir)) { boolean truncated = truncLog.truncate(zxid); // re-open the txnLog and snapLog // I'd rather just close/reopen this object itself, however that // would have a big impact outside ZKDatabase as there are other // objects holding a reference to this object. txnLog = new FileTxnLog(dataDir); snapLog = new FileSnap(snapDir); return truncated; } } catch (IOException e) { LOG.error("Unable to truncate Txn log", e); return false; } }
Source:
public void close() throws IOException { txnLog.close(); START txnLog = null; END snapLog.close(); snapLog = null; } if you set this variable to null then calling twice this method will result in an ugly NullPointerException what about ``` try { if (txnLog != null) { txnLog.close(); } } finally { txnLog = null; } ```

Target:
public void close() throws IOException { if (txnLog != null) { txnLog.close(); txnLog = null; } if (snapLog != null) { snapLog.close(); snapLog = null; } }
Source:
public void testBooleanRetrievalFromProperty() { boolean value = true; boolean defaultValue = false; x509Util = new ClientX509Util(); System.setProperty(x509Util.getSslProtocolProperty(), Boolean.toString(value)); ZKConfig conf = new ZKConfig(); boolean result = conf.getBoolean(x509Util.getSslProtocolProperty(), defaultValue); assertEquals(value, result); START System.clearProperty(x509Util.getSslProtocolProperty()); END } Clear properties in tearDown method @After public void tearDown() throws Exception { }

Target:
public void testBooleanRetrievalFromProperty() { boolean value = true; boolean defaultValue = false; System.setProperty(x509Util.getSslProtocolProperty(), Boolean.toString(value)); ZKConfig conf = new ZKConfig(); boolean result = conf.getBoolean(x509Util.getSslProtocolProperty(), defaultValue); assertEquals(value, result); }
Source:
public ContainerManager(ZKDatabase zkDb, RequestProcessor requestProcessor, int checkIntervalMs, int maxPerMinute) { this(zkDb, requestProcessor, checkIntervalMs, maxPerMinute, Integer.MAX_VALUE); START } END Long.MAX_VALUE ?

Target:
public ContainerManager(ZKDatabase zkDb, RequestProcessor requestProcessor, int checkIntervalMs, int maxPerMinute) { this(zkDb, requestProcessor, checkIntervalMs, maxPerMinute, Long.MAX_VALUE); }
Source:
START public void addEntry(FieldName fieldName, String value) { END if (value != null) { logEntries.put(fieldName.name().toLowerCase(), value); } } package-private ?

Target:
void addEntry(FieldName fieldName, String value) { if (value != null) { logEntries.put(fieldName.name().toLowerCase(), value); } }
Source:
void closeSockSync() { try { long startTime = Time.currentElapsedTime(); START sock.close(); END ServerMetrics.getMetrics().SOCKET_CLOSING_TIME.add(Time.currentElapsedTime() - startTime); } catch (IOException e) { LOG.warn("Ignoring error closing connection to leader", e); } } You might want to add 2 more things here: - double check if sock is still not `null` - make sock = null after the close to prevent further usage of the object and let GC collect it

Target:
void closeSockSync() { try { long startTime = Time.currentElapsedTime(); if (sock != null) { sock.close(); sock = null; } ServerMetrics.getMetrics().SOCKET_CLOSING_TIME.add(Time.currentElapsedTime() - startTime); } catch (IOException e) { LOG.warn("Ignoring error closing connection to leader", e); } }
Source:
public ClientCnxn( String chrootPath, HostProvider hostProvider, int sessionTimeout, ZKClientConfig clientConfig, Watcher defaultWatcher, ClientCnxnSocket clientCnxnSocket, boolean canBeReadOnly ) { this( chrootPath, hostProvider, sessionTimeout, clientConfig, START defaultWatcher, END clientCnxnSocket, 0, new byte[16], canBeReadOnly); } On first glance it doesn't look like it conflicts with Curator. But, maybe @TisonKun can test with Curator locally. As an aside, I wonder if we shouldn't include some Curator test in ZooKeeper in the future - i.e. validate that new changes don't break Curator.

Target:
public ClientCnxn( String chrootPath, HostProvider hostProvider, int sessionTimeout, ZKClientConfig clientConfig, Watcher defaultWatcher, ClientCnxnSocket clientCnxnSocket, boolean canBeReadOnly ) throws IOException { this( chrootPath, hostProvider, sessionTimeout, clientConfig, defaultWatcher, clientCnxnSocket, 0, new byte[16], canBeReadOnly); }
Source:
public void testRestoreWithTrustedEmptySnapFiles() throws Exception { START FileTxnSnapLog.setTrustEmptySnapshotFlag(true); END runTest(false, true); assertFalse("Trust empty snapshot flag should be reset after first use.", FileTxnSnapLog.getTrustEmptySnapshotFlag()); } what about resetting the value to "false" in a finally block, this way if the test fails we will be sure that the rest of the tests are executed with the default value

Target:
public void testRestoreWithTrustedEmptySnapFiles() throws Exception { runTest(false, true); }
Source:
public synchronized void startup() { if (sessionTracker == null) { createSessionTracker(); } startSessionTracker(); setupRequestProcessors(); startRequestThrottler(); registerJMX(); startJvmPauseMonitor(); registerMetrics(); setState(State.RUNNING); requestPathMetricsCollector.start(); START localSessionEnabled = sessionTracker instanceof UpgradeableSessionTracker END && ((UpgradeableSessionTracker) sessionTracker).isLocalSessionsEnabled(); notifyAll(); } this 'instanceof' looks like a 'code smell' what about adding a isLocalSessionsEnabled method to the base class (SessionTracker) with a default return value of 'false' ?

Target:
public synchronized void startup() { if (sessionTracker == null) { createSessionTracker(); } startSessionTracker(); setupRequestProcessors(); startRequestThrottler(); registerJMX(); startJvmPauseMonitor(); registerMetrics(); setState(State.RUNNING); requestPathMetricsCollector.start(); localSessionEnabled = sessionTracker.isLocalSessionsEnabled(); notifyAll(); }
Source:
public void trackSent(long timestamp) { START if (enabled) sentBuffer.write(new BufferedMessage(timestamp)); END } Should we keep the same coding format and change it to: if (enabled) { ... }

Target:
public void trackSent(long timestamp) { if (enabled) { sentBuffer.write(new BufferedMessage(timestamp)); } }
Source:
protected abstract KeyStore keyStoreInstance() throws KeyStoreException; START private char[] passwordStringToCharArray(String password) { END return password == null ? EMPTY_CHAR_ARRAY : password.toCharArray(); } Nit: static

Target:
protected abstract KeyStore keyStoreInstance() throws KeyStoreException; private static char[] passwordStringToCharArray(String password) { return password == null ? EMPTY_CHAR_ARRAY : password.toCharArray(); }
Source:
void closeSocket() { try { START if (sock != null && !sock.isClosed()) { END sock.close(); } } catch (IOException e) { LOG.warn("Ignoring error closing connection to leader", e); } } Just curious: why checking 'isClosed' ? Can't we call 'close' multiple times?

Target:
void closeSocket() { try { if (sock != null) { sock.close(); } } catch (IOException e) { LOG.warn("Ignoring error closing connection to leader", e); } }
Source:
START public int requestThrottleInflight() { END if (requestThrottler != null) { return requestThrottler.getInflight(); } return 0; } consider make this private as it's not called elsewhere.

Target:
private int requestThrottleInflight() { if (requestThrottler != null) { return requestThrottler.getInflight(); } return 0; }
Source:
public void tearDown() throws Exception { START zkClient.close(); END zkClientB.close(); super.tearDown(); } Please add null check for the case when `setUp()` fails early.

Target:
public void tearDown() throws Exception { if (zkClient != null) { zkClient.close(); } if (zkClientB != null) { zkClientB.close(); } super.tearDown(); }
Source:
static void waitForReconnectDelay() { START Observer.waitForReconnectDelayHelper(reconnectDelayMs); END } Nit: no need to call Observer explicitly

Target:
static void waitForReconnectDelay() { waitForReconnectDelayHelper(reconnectDelayMs); }
Source:
public int getCount() { return (int) getValue(countStr); START } END why are we casting to "int" ? it the value is a "long" we must return a "long" or we have to handle the overflow some way

Target:
public long getCount() { return getValue(countStr); }
Source:
public int hashCode() { START return Objects.hash(super.hashCode(), time); END } This is a private class used for tracking, won't change super fields, time is the only changing variable here, so only need to hash time here.

Target:
public int hashCode() { return Objects.hash(time); }
Source:
public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; START if (!super.equals(o)) return false; END MarkerQuorumPacket that = (MarkerQuorumPacket) o; return time == that.time; } Don't need to compare super as what I mentioned above.

Target:
public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; MarkerQuorumPacket that = (MarkerQuorumPacket) o; return time == that.time; }
Source:
public boolean equals(Object o) { if (this == o) { return true; } START if (o == null || getClass() != o.getClass()) { END return false; } ErrorResult that = (ErrorResult) o; return err == that.err && Objects.equals(path, that.path); } why are you changing this line ?

Target:
public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof ErrorResult)) { return false; } ErrorResult that = (ErrorResult) o; return err == that.err && Objects.equals(path, that.path); }
Source:
public void start() { START if (listenBacklog > 0) { END bootstrap.option(ChannelOption.SO_BACKLOG, listenBacklog); } LOG.info("binding to port {}", localAddress); parentChannel = bootstrap.bind(localAddress).syncUninterruptibly().channel(); // Port changes after bind() if the original port was 0, update // localAddress to get the real port. localAddress = (InetSocketAddress) parentChannel.localAddress(); LOG.info("bound to port " + getLocalPort()); } It doesn't make any difference, but it would nice for this condition to be consistent with the NIO implementation. e.g. `listenBacklog != -1` or something similar

Target:
public void start() { if (listenBacklog != -1) { bootstrap.option(ChannelOption.SO_BACKLOG, listenBacklog); } LOG.info("binding to port {}", localAddress); parentChannel = bootstrap.bind(localAddress).syncUninterruptibly().channel(); // Port changes after bind() if the original port was 0, update // localAddress to get the real port. localAddress = (InetSocketAddress) parentChannel.localAddress(); LOG.info("bound to port " + getLocalPort()); }
Source:
public void setClientPortListenBacklog(int backlog) { START LOG.info("clientPortListenBacklog set to " + backlog); END this.listenBacklog = backlog; } nit: I would do the action first, than the log the fact it happened.

Target:
public void setClientPortListenBacklog(int backlog) { this.listenBacklog = backlog; LOG.info("clientPortListenBacklog set to " + backlog); }
Source:
public SSLContext get() { try { return SSLContext.getDefault(); } catch (NoSuchAlgorithmException e) { START return null; END } } nit: `throw new RuntimeException(e);` so if this ever happens (unlikely), the error will be more obvious in the test

Target:
public SSLContext get() { try { return SSLContext.getDefault(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
Source:
public void testWriteBool() { final boolean expected = false; final String tag = "tag1"; checkWriterAndReader( (oa) -> oa.writeBool(expected, tag), (ia) -> { boolean actual = ia.readBool(tag); START Assert.assertEquals(expected, actual); END } ); } Sorry, last nit: any reason for not using static imports?

Target:
public void testWriteBool() { final boolean expected = false; final String tag = "tag1"; checkWriterAndReader( (oa) -> oa.writeBool(expected, tag), (ia) -> { boolean actual = ia.readBool(tag); assertEquals(expected, actual); } ); }
Source:
START public Boolean serializeZxidDigest(OutputArchive oa) throws IOException { END if (!DigestCalculator.digestEnabled()) { return false; } ZxidDigest zxidDigest = lastProcessedZxidDigest; if (zxidDigest == null) { // write an empty digest zxidDigest = new ZxidDigest(); } zxidDigest.serialize(oa); return true; } Nit: boolean not Boolean

Target:
public boolean serializeZxidDigest(OutputArchive oa) throws IOException { if (!DigestCalculator.digestEnabled()) { return false; } ZxidDigest zxidDigest = lastProcessedZxidDigest; if (zxidDigest == null) { // write an empty digest zxidDigest = new ZxidDigest(); } zxidDigest.serialize(oa); return true; }
Source:
START public Boolean deserializeZxidDigest(InputArchive ia) throws IOException { END if (!DigestCalculator.digestEnabled()) { return false; } try { ZxidDigest zxidDigest = new ZxidDigest(); zxidDigest.deserialize(ia); if (zxidDigest.zxid > 0) { digestFromLoadedSnapshot = zxidDigest; } return true; } catch (EOFException e) { LOG.warn("Got EOF exception while reading the digest, " + "likely due to the reading an older snapshot."); return false; } } Nit: boolean

Target:
public boolean deserializeZxidDigest(InputArchive ia) throws IOException { if (!DigestCalculator.digestEnabled()) { return false; } try { ZxidDigest zxidDigest = new ZxidDigest(); zxidDigest.deserialize(ia); if (zxidDigest.zxid > 0) { digestFromLoadedSnapshot = zxidDigest; } return true; } catch (EOFException e) { LOG.warn("Got EOF exception while reading the digest, " + "likely due to the reading an older snapshot."); return false; } }
Source:
START public default void inc() { END inc(1); } public keyword is not needed in interfaces. All of them are public by default

Target:
default void inc() { inc(1); }
Source:
public void testOrderWithDifferentPrefixes() throws Exception { String[] names = { "r-3", "r-2", "r-1", "w-2", "w-1" }; // names with duplicated sequence numbers are not included in the result START String[] expected = { "r-1", "r-2", "r-3" }; END assertOrderedNodeNames(names, expected); } Why have you removed "w-1" and "w-2" elements from here? I think the expected list could be: `String[] expected = { "r-1", "w-1", "r-2", "w-2", "r-3"};`

Target:
public void testOrderWithDifferentPrefixes() throws Exception { String[] names = { "r-3", "r-2", "r-1", "w-2", "w-1" }; String[] expected = { "r-1", "w-1", "r-2", "w-2", "r-3" }; assertOrderedNodeNames(names, expected); }
Source:
public boolean close(int waitForShutdownTimeoutMs) throws InterruptedException { close(); START if (waitForShutdownTimeoutMs > 0) { END return testableWaitForShutdown(waitForShutdownTimeoutMs); } return false; } I don't think this check is needed - actually, it sounds better to remove this check so a negative value will trigger illegal argument exception indicates something is wrong in upstream code, because it's hard to know if the negative value is passed on purpose, or is an accident.

Target:
public boolean close(int waitForShutdownTimeoutMs) throws InterruptedException { close(); return testableWaitForShutdown(waitForShutdownTimeoutMs); }
Source:
START private void createSocket() throws X509Exception, IOException { END if (self.isSslQuorum()) { if (x509Util == null) { x509Util = new QuorumX509Util(); } sock = x509Util.createSSLSocket(); } else { sock = new Socket(); } sock.setSoTimeout(self.tickTime * self.initLimit); } minor style nit: prefer you return the socket from this method and assign to `sock` in the caller.

Target:
private Socket createSocket() throws X509Exception, IOException { Socket sock; if (self.isSslQuorum()) { if (x509Util == null) { x509Util = new QuorumX509Util(); } sock = x509Util.createSSLSocket(); } else { sock = new Socket(); } sock.setSoTimeout(self.tickTime * self.initLimit); return sock; }
Source:
private void configureSSLServerSocket(SSLServerSocket sslServerSocket) { SSLParameters sslParameters = sslServerSocket.getSSLParameters(); sslParameters.setNeedClientAuth(true); START if (cipherSuites != null) { END LOG.debug("Setup cipher suites for server socket: {}", Arrays.toString(cipherSuites)); sslParameters.setCipherSuites(cipherSuites); } sslServerSocket.setSSLParameters(sslParameters); } do we need this check here? `cipherSuites` should not be null when we reach here.

Target:
private void configureSSLServerSocket(SSLServerSocket sslServerSocket) { SSLParameters sslParameters = sslServerSocket.getSSLParameters(); sslParameters.setNeedClientAuth(true); LOG.debug("Setup cipher suites for server socket: {}", Arrays.toString(cipherSuites)); sslParameters.setCipherSuites(cipherSuites); sslServerSocket.setSSLParameters(sslParameters); }
Source:
private void configureSSLSocket(SSLSocket sslSocket) { START if (cipherSuites != null) { END SSLParameters sslParameters = sslSocket.getSSLParameters(); LOG.debug("Setup cipher suites for client socket: {}", Arrays.toString(cipherSuites)); sslParameters.setCipherSuites(cipherSuites); sslSocket.setSSLParameters(sslParameters); } } do we need this check here? cipherSuites should not be null when we reach here.

Target:
private void configureSSLSocket(SSLSocket sslSocket) { SSLParameters sslParameters = sslSocket.getSSLParameters(); LOG.debug("Setup cipher suites for client socket: {}", Arrays.toString(cipherSuites)); sslParameters.setCipherSuites(cipherSuites); sslSocket.setSSLParameters(sslParameters); }
Source:
public void testFourLetterWordsAllDisabledByDefault() throws Exception { stopServer(); FourLetterCommands.resetWhiteList(); System.setProperty("zookeeper.4lw.commands.whitelist", "stat"); startServer(); // Default white list for 3.5.x is empty, so all command should fail. verifyAllCommandsFail(); TestableZooKeeper zk = createClient(); START String sid = getHexSessionId(zk.getSessionId()); END verifyAllCommandsFail(); zk.getData("/", true, null); verifyAllCommandsFail(); zk.close(); verifyFuzzyMatch("stat", "Outstanding"); verifyAllCommandsFail(); } sid is not used

Target:
public void testFourLetterWordsAllDisabledByDefault() throws Exception { stopServer(); FourLetterCommands.resetWhiteList(); System.setProperty("zookeeper.4lw.commands.whitelist", "stat"); startServer(); // Default white list for 3.5.x is empty, so all command should fail. verifyAllCommandsFail(); TestableZooKeeper zk = createClient(); verifyAllCommandsFail(); zk.getData("/", true, null); verifyAllCommandsFail(); zk.close(); verifyFuzzyMatch("stat", "Outstanding"); verifyAllCommandsFail(); }
Source:
public void testBasic() throws IOException, InterruptedException, KeeperException { ZooKeeper zk = null; try { zk = createClient(new CountdownWatcher(), hostPort); zk.addPersistentWatch("/a/b", persistentWatcher); internalTestBasic(zk); } finally { if (zk != null) { START zk.close(); END } } } ZooKeeper supports try-with-resources so using that may make this code cleaner

Target:
public void testBasic() throws IOException, InterruptedException, KeeperException { try ( ZooKeeper zk = createClient(new CountdownWatcher(), hostPort) ) { zk.addPersistentWatch("/a/b", persistentWatcher, false); internalTestBasic(zk); } }
Source:
START private boolean shouldAllowSaslFailedClientsConnect() { END return Boolean.getBoolean(ALLOW_SASL_FAILED_CLIENTS); } Last nit: static

Target:
private static boolean shouldAllowSaslFailedClientsConnect() { return Boolean.getBoolean(ALLOW_SASL_FAILED_CLIENTS); }
Source:
START public void setup() throws InterruptedException, KeeperException.NoNodeException { END System.setProperty(authProvider, superDigest); QuorumPeerConfig.setReconfigEnabled(false); // Get a three server quorum. qu = new QuorumUtil(1); qu.disableJMXTest = true; try { qu.startAll(); } catch (IOException e) { Assert.fail("Fail to start quorum servers."); } resetZKAdmin(); } we don't throw NoNodeException anymore

Target:
public void setup() throws InterruptedException { System.setProperty(authProvider, superDigest); QuorumPeerConfig.setReconfigEnabled(false); // Get a three server quorum. qu = new QuorumUtil(1); qu.disableJMXTest = true; try { qu.startAll(); } catch (IOException e) { Assert.fail("Fail to start quorum servers."); } resetZKAdmin(); }
Source:
START public ClientCnxnSocketNIO(ZKClientConfig clientConfig) throws IOException { END this.clientConfig = clientConfig; initProperties(); } You're probably making this public for test purposes. Instead of making such methods public, we should be having the tests in the appropriate package so that we don't need to use the access modifiers inappropriately. I say inappropriately because this method shouldn't really be exposed to applications, it should not be public.

Target:
ClientCnxnSocketNIO(ZKClientConfig clientConfig) throws IOException { this.clientConfig = clientConfig; initProperties(); }
Source:
public static boolean isTransactionFile(String file) throws IOException { try { START RandomAccessFileReader reader = new RandomAccessFileReader(new File(file)); END BinaryInputArchive logStream = new BinaryInputArchive(reader); FileHeader fhdr = new FileHeader(); fhdr.deserialize(logStream, "fileheader"); } finally { reader.close(); } return fhdr.getMagic() == FileTxnLog.TXNLOG_MAGIC; } Mixture of spaces and tab on this line and several others. Do we enforce any sort of consistency when it comes to formatting in this project? I seem to see both.

Target:
public static boolean isTransactionFile(String file) throws IOException { RandomAccessFileReader reader = new RandomAccessFileReader(new File(file)); try { BinaryInputArchive logStream = new BinaryInputArchive(reader); FileHeader fhdr = new FileHeader(); fhdr.deserialize(logStream, "fileheader"); } finally { reader.close(); } return fhdr.getMagic() == FileTxnLog.TXNLOG_MAGIC; }
Source:
public void fireEventListeners(final Event event) { final ArrayList<EventListener> listeners = new ArrayList<>(this.listeners); for (final EventListener listener : listeners) { try { listener.handleEvent(event); START } catch (Exception e) { END logger.warn("Error while calling handleEvent for: " + listener.getClass()); logger.warn(e.getMessage(), e); } } } If you use java.lang.RuntimeException and java.lang.Error, please include those here.

Target:
public void fireEventListeners(final Event event) { final ArrayList<EventListener> listeners = new ArrayList<>(this.listeners); for (final EventListener listener : listeners) { try { listener.handleEvent(event); } catch (RuntimeException e) { logger.warn("Error while calling handleEvent for: " + listener.getClass()); logger.warn(e.getMessage(), e); } } }
Source:
public static DateTimeZone getTimeZone(String timezoneParam) { try { return DateTimeZone.forID(timezoneParam); } catch (final IllegalArgumentException e) { TimeZone zone = ZoneInfo.getTimeZone(timezoneParam); START if(null == zone) return null; END try { return DateTimeZone.forTimeZone(zone); } catch (final IllegalArgumentException e1) { return null; } } } Can you write this in curly braces block instead of 1 line?

Target:
public static DateTimeZone getTimeZone(String timezoneParam) { try { return DateTimeZone.forID(timezoneParam); } catch (final IllegalArgumentException e) { TimeZone zone = ZoneInfo.getTimeZone(timezoneParam); if(null == zone) { return null; } try { return DateTimeZone.forTimeZone(zone); } catch (final IllegalArgumentException e1) { return null; } } }
Source:
public void tearDown() throws Exception { START if (this.fileWatcher != null) { END this.fileWatcher.close(); } // Cleanup directories final Path workDir = PATH.getParent(); if (Files.exists(workDir)) { Files.deleteIfExists(PATH); Files.delete(workDir); } } How about using a `TemporaryFolder` `@Rule` for the workDir to simplify code? For example here: https://github.com/azkaban/azkaban/blob/7efe67b0602fdc65f18fc0e2f91e14a57c2d845e/azkaban-exec-server/src/test/java/azkaban/execapp/event/LocalFlowWatcherTest.java#L31-L32

Target:
public void tearDown() throws Exception { if (this.fileWatcher != null) { this.fileWatcher.close(); } }
Source:
public void shutdown() { START if(null != this.queueProcessor) this.queueProcessor.shutdown(); END if(null != this.updaterThread) this.updaterThread.shutdown(); } Can you please follow coding guidelines and put the code in curly braces?

Target:
public void shutdown() { if(null != this.queueProcessor) { this.queueProcessor.shutdown(); } if(null != this.updaterThread) { this.updaterThread.shutdown(); } }
Source:
private static FileSystem getCachedHttpFileSystem(final Configuration conf, final AzkabanCommonModuleConfig azConfig) { // Ensure the necessary props are not specified to enable CachedHttpFileSystem if (azConfig.getOriginDependencyRootUri() == null) return null; START else validateURI(azConfig.getOriginDependencyRootUri(), CHTTP_SCHEME); END try { return FileSystem.get(azConfig.getOriginDependencyRootUri(), conf); } catch (final IOException e) { log.error("Unable to initialize CachedHttpFileSystem.", e); throw new AzkabanException(e); } } Please follow the style guide mentioned in CONTRIBUTING.md, use braces for if/else statements, https://google.github.io/styleguide/javaguide.html#s4-formatting

Target:
private static FileSystem getCachedHttpFileSystem(final Configuration conf, final AzkabanCommonModuleConfig azConfig) { // Ensure the necessary props are not specified to enable CachedHttpFileSystem if (azConfig.getOriginDependencyRootUri() == null) { return null; } // Ensure the origin URI has the correct scheme and has an authority. validateURI(azConfig.getOriginDependencyRootUri(), CHTTP_SCHEME, true); try { return FileSystem.get(azConfig.getOriginDependencyRootUri(), conf); } catch (final IOException e) { log.error("Unable to initialize CachedHttpFileSystem.", e); throw new AzkabanException(e); } }
Source:
START public String getFileName() { return fileName; } END use keyword "this" in these methods before member names.

Target:
public String getFileName() { return this.fileName; }
Source:
public URI getUri() { START return uri; END } this.uri

Target:
public URI getUri() { return this.uri; }
Source:
public List<String> getGroupsWithPermission(final Type type) { START final ArrayList<String> groups = new ArrayList<>(); END for (final Map.Entry<String, Permission> entry : this.groupPermissionMap.entrySet()) { final Permission perm = entry.getValue(); if (perm.isPermissionSet(type)) { groups.add(entry.getKey()); } } return groups; } Use " List<String>" instead of ArrayList<String> in declaration.

Target:
public List<String> getGroupsWithPermission(final Type type) { final List<String> groups = new ArrayList<>(); for (final Map.Entry<String, Permission> entry : this.groupPermissionMap.entrySet()) { final Permission perm = entry.getValue(); if (perm.isPermissionSet(type)) { groups.add(entry.getKey()); } } return groups; }
Source:
private void setNeedsMoreUsersIfAppropriate(final Page page, final Project project, final Session session) { // See if the project has only one admin user with no admin groups. // If it does AND the forceMultipleUsers flag is set AND the current user has the ability to add // an admin, let's force the user to do so before allowing any execution or scheduling of flows. boolean hasEnoughUsersGroups = project.getUsersWithPermission(Type.ADMIN).size() > 1 || project.getGroupsWithPermission(Type.ADMIN).size() > 0 || !hasPermission(session.getUser(), Type.ADMIN) START || !this.forceMultipleUsers; END page.add("hasEnoughUsersGroups", hasEnoughUsersGroups); } nitpick : move this.forceMultipleUsers to the front, if the config is off, we fallback to existing logic.

Target:
private void setNeedsMoreUsersIfAppropriate(final Page page, final Project project, final Session session) { // See if the project has only one admin user with no admin groups. // If it does AND the forceMultipleUsers flag is set AND the current user has the ability to add // an admin, let's force the user to do so before allowing any execution or scheduling of flows. boolean hasEnoughUsersGroups = !this.forceMultipleUsers || project.getUsersWithPermission(Type.ADMIN).size() > 1 || project.getGroupsWithPermission(Type.ADMIN).size() > 0 || !hasPermission(session.getUser(), Type.ADMIN); page.add("hasEnoughUsersGroups", hasEnoughUsersGroups); }
Source:
public Object isSlaFailed() { final ExecutableFlow flow; try { flow = this.executorLoader.fetchExecutableFlow(this.execId); START logger.info("Flow for execid " + this.execId + " is " + flow.toString()); END } catch (final ExecutorManagerException e) { logger.error("Can't get executable flow.", e); // something wrong, send out alerts return true; } return isSlaMissed(flow); } Why to you need to print out `flow.toString()`? Would `flow.getId()` be better?

Target:
public Object isSlaFailed() { final ExecutableFlow flow; try { flow = this.executorLoader.fetchExecutableFlow(this.execId); logger.info("Flow for execid " + this.execId + " is " + flow.getId()); } catch (final ExecutorManagerException e) { logger.error("Can't get executable flow.", e); // something wrong, send out alerts return true; } return isSlaMissed(flow); }
Source:
START public static State createInstance(long startTime, long endTime, long lastUpdatedTime, END int numTrail, int numSuccess, int numFailure, int numIgnored, boolean isPaused, int rampStage, boolean isActive) { return new State() .setStartTime(startTime) .setEndTime(endTime) .setLastUpdatedTime(lastUpdatedTime) .setNumOfTrail(numTrail) .setNumOfSuccess(numSuccess) .setNumOfFailure(numFailure) .setNumOfIgnored(numIgnored) .setPaused(isPaused) .setRampStage(rampStage) .setActive(isActive); } Add `final` keyword? Also other places when necessary.

Target:
public static final State createInstance(long startTime, long endTime, long lastUpdatedTime, int numTrail, int numSuccess, int numFailure, int numIgnored, boolean isPaused, int rampStage, boolean isActive) { return new State() .setStartTime(startTime) .setEndTime(endTime) .setLastUpdatedTime(lastUpdatedTime) .setNumOfTrail(numTrail) .setNumOfSuccess(numSuccess) .setNumOfFailure(numFailure) .setNumOfIgnored(numIgnored) .setPaused(isPaused) .setRampStage(rampStage) .setActive(isActive); }
Source:
START public void setLocked(boolean locked) { isLocked = locked; } END Please follow the style guide in CONTRIBUTING.md to add `this` to field access. You can turn on > save action .

Target:
public void setLocked(boolean locked) { this.isLocked = locked; }
Source:
START public double getCpuLoad() { END if (this.collectedCpuStats.isEmpty()) { return -1; } final CpuStats oldestCpuStats = this.collectedCpuStats.pollLast(); final CpuStats newestCpuStats = getCpuStats(); this.collectedCpuStats.push(newestCpuStats); return calcCpuLoad(oldestCpuStats, newestCpuStats); } Is calculating the newest and oldest data points the right way to get CPU load? Let's discuss more about it.

Target:
public double getCpuLoad() { if (this.collectedCpuStats.isEmpty()) { return -1; } final CpuStats newestCpuStats = getCpuStats(); if (newestCpuStats == null) { return -1; } final CpuStats oldestCpuStats = this.collectedCpuStats.pollLast(); this.collectedCpuStats.push(newestCpuStats); return calcCpuLoad(oldestCpuStats, newestCpuStats); }
Source:
START public String getRunningFlowIds() { END final List<Integer> allIds = new ArrayList<>(); try { getExecutionIdsHelper(allIds, this.executorLoader.fetchUnfinishedFlows().values()); } catch (final ExecutorManagerException e) { this.logger.error("Failed to get running flow ids.", e); } return allIds.toString(); } why returning a string instead of a list?

Target:
public List<Integer> getRunningFlowIds() { final List<Integer> allIds = new ArrayList<>(); try { getExecutionIdsHelper(allIds, this.executorLoader.fetchUnfinishedFlows().values()); } catch (final ExecutorManagerException e) { this.logger.error("Failed to get running flow ids.", e); } return allIds; }
Source:
START public String getQueuedFlowIds() { END final List<Integer> allIds = new ArrayList<>(); try { getExecutionIdsHelper(allIds, this.executorLoader.fetchQueuedFlows()); } catch (final ExecutorManagerException e) { this.logger.error("Failed to get queued flow ids.", e); } return allIds.toString(); } same question as above.

Target:
public List<Integer> getQueuedFlowIds() { final List<Integer> allIds = new ArrayList<>(); try { getExecutionIdsHelper(allIds, this.executorLoader.fetchQueuedFlows()); } catch (final ExecutorManagerException e) { this.logger.error("Failed to get queued flow ids.", e); } return allIds; }
Source:
START public int getDisplayExecutionPageSize() { END return displayExecutionPageSize; } We may don't need to introduce `public` here? package-access is good enough?

Target:
protected int getDisplayExecutionPageSize() { return displayExecutionPageSize; }
Source:
public static void setupFolder() { workingDirString = currentDirString + "/../temp/TestHadoopSpark"; workingDirFile = new File(workingDirString); libFolderFile = new File(workingDirFile, "lib"); START libFolderFile = new File(workingDirFile, "lib"); END executionJarFile = new File(libFolderFile, "hadoop-spark-job-test-execution-x.y.z-a.b.c" + ".jar"); libraryJarFile = new File(libFolderFile, "library.jar"); } duplicate?

Target:
public static void setupFolder() { workingDirString = currentDirString + "/../temp/TestHadoopSpark"; workingDirFile = new File(workingDirString); libFolderFile = new File(workingDirFile, "lib"); executionJarFile = new File(libFolderFile, "hadoop-spark-job-test-execution-x.y.z-a.b.c" + ".jar"); libraryJarFile = new File(libFolderFile, "library.jar"); }
Source:
public boolean containsKey(final String key) { START return super.get(key.toLowerCase()) != null; END } super.containsKey(key.toLowerCase()) ? given ConcurrentHashMap already provides containsKey

Target:
public boolean containsKey(final String key) { return this.map.containsKey(key.toLowerCase()); }
Source:
private void write(final String string) { // If logs are looping, write message to system's stderr then rebind. START if (logIsLooping()) { END unbindStdOutAndErrToLog(); System.err.println(string); bindStdOutAndErrToLog(); } else { logger.log(this.level, string); } } How big is the overhead to do this check on every log?

Target:
private void write(final String string) { logger.log(this.level, string); }
Source:
public List<byte[]> handle(final ResultSet rs) throws SQLException { if (!rs.next()) { return Collections.emptyList(); } START final ArrayList<byte[]> data = new ArrayList<>(); END do { final byte[] bytes = rs.getBytes(1); data.add(bytes); } while (rs.next()); return data; } List<byte[]> ?

Target:
public List<byte[]> handle(final ResultSet rs) throws SQLException { if (!rs.next()) { return Collections.emptyList(); } final List<byte[]> data = new ArrayList<>(); do { final byte[] bytes = rs.getBytes(1); data.add(bytes); } while (rs.next()); return data; }
Source:
public void start() { try { this.scheduler.start(); } catch (final SchedulerException e) { START logger.warn("Error starting Quartz scheduler: ", e); END } logger.info("Quartz Scheduler started."); } warn seems too relaxing for this kind of exception. maybe error?

Target:
public void start() { try { this.scheduler.start(); } catch (final SchedulerException e) { logger.error("Error starting Quartz scheduler: ", e); } logger.info("Quartz Scheduler started."); }
Source:
START Scheduler getScheduler() { END return this.scheduler; } access modifiers?

Target:
public Scheduler getScheduler() { return this.scheduler; }
Source:
public void shutdownNow() { logger.warn("Shutting down FlowRunnerManager now..."); this.executorService.shutdownNow(); this.triggerManager.shutdown(); START deleteExecutionDirectory(); END } My mistake, I mis-interpreted the call to the shotdown method for the triggerManager as the call for the FlowRunnerManager.

Target:
public void shutdownNow() { logger.warn("Shutting down FlowRunnerManager now..."); this.executorService.shutdownNow(); this.triggerManager.shutdown(); }
Source:
public void testInjection() throws Exception { props .put(Constants.ConfigurationKeys.AZKABAN_STORAGE_LOCAL_BASEDIR, AZKABAN_LOCAL_TEST_STORAGE); final Injector injector = Guice.createInjector( new AzkabanCommonModule(props), new AzkabanExecServerModule() ); START SERVICE_PROVIDER.unsetInjector(); END SERVICE_PROVIDER.setInjector(injector); assertNotNull(SERVICE_PROVIDER.getInstance(AzkabanExecutorServer.class)); } Why is this change necessary?

Target:
public void testInjection() throws Exception { props .put(Constants.ConfigurationKeys.AZKABAN_STORAGE_LOCAL_BASEDIR, AZKABAN_LOCAL_TEST_STORAGE); final Injector injector = Guice.createInjector( new AzkabanCommonModule(props), new AzkabanExecServerModule() ); assertNotNull(injector.getInstance(AzkabanExecutorServer.class)); }
Source:
public void doAction() throws Exception { logger.info("ready to do action " + getDescription()); START FlowRunnerManager flowRunnerManager = ServiceProvider.SERVICE_PROVIDER.getInstance(FlowRunnerManager.class); END flowRunnerManager.cancelJobBySLA(execId, jobId); } Should `flowRunnerManager ` be initialized in the body of constructor?

Target:
public void doAction() throws Exception { logger.info("ready to do action " + getDescription()); final FlowRunnerManager flowRunnerManager = ServiceProvider.SERVICE_PROVIDER .getInstance(FlowRunnerManager.class); flowRunnerManager.cancelJobBySLA(this.execId, this.jobId); }
Source:
private static String getSqlScriptsDir() throws IOException { // Dummy because any resource file works. final String dummyResourcePath = getUserManagerXmlFile(); Path resources = Paths.get(dummyResourcePath).getParent(); Path azkabanRoot = resources.getParent().getParent().getParent().getParent(); File sqlScriptDir = Paths.get(azkabanRoot.toString(), AZKABAN_DB_SQL_PATH).toFile(); START return props.getString(AzkabanDatabaseSetup.DATABASE_SQL_SCRIPT_DIR, sqlScriptDir.getCanonicalPath()); END } Why do you need to care about what the entry is in the props? The test is in full control of what's in the props. Right?

Target:
private static String getSqlScriptsDir() throws IOException { // Dummy because any resource file works. final String dummyResourcePath = getUserManagerXmlFile(); Path resources = Paths.get(dummyResourcePath).getParent(); Path azkabanRoot = resources.getParent().getParent().getParent().getParent(); File sqlScriptDir = Paths.get(azkabanRoot.toString(), AZKABAN_DB_SQL_PATH).toFile(); return sqlScriptDir.getCanonicalPath(); }
Source:
public DatabaseOperatorImpl(QueryRunner queryRunner){ this.queryRunner = queryRunner; START requireNonNull(queryRunner.getDataSource(), "data source must not be null."); END } i think it's better to put requireNonNull(queryRunner.getDataSource(), "data source must not be null.") before this.queryRunner = queryRunner.

Target:
public DatabaseOperatorImpl(QueryRunner queryRunner){ requireNonNull(queryRunner.getDataSource(), "data source must not be null."); this.queryRunner = queryRunner; }
Source:
public Boolean isActiveProject(int id) { Project project = getProject(id); START return project != null; END } getProject(id) != null simpler

Target:
public Boolean isActiveProject(int id) { return getProject(id) != null; }
Source:
public void testRemovingExecutor() throws Exception { if (!isTestSetup()) { return; } ExecutorLoader loader = createLoader(); Executor executor = loader.addExecutor("localhost1", 12345); loader.removeExecutor("localhost1", 12345); START Executor fetchedExecutor = loader.fetchExecutor("localhost1", 12345); END Assert.assertNull(fetchedExecutor); } May be its safer to assert that it exists before and then assert that is doesn't after delete.

Target:
public void testRemovingExecutor() throws Exception { if (!isTestSetup()) { return; } ExecutorLoader loader = createLoader(); Executor executor = loader.addExecutor("localhost1", 12345); Assert.assertNotNull(executor); loader.removeExecutor("localhost1", 12345); Executor fetchedExecutor = loader.fetchExecutor("localhost1", 12345); Assert.assertNull(fetchedExecutor); }
Source:
private void attachKafkaAppender(KafkaLog4jAppender appender) { // If present, remove the existing kafka appender START if (kafkaAppender.isPresent()) { END removeAppender(kafkaAppender); flowLogger.info("Removed old Kafka appender for job " + this.jobId); } kafkaAppender = Optional.of(appender); logger.addAppender(kafkaAppender.get()); logger.setAdditivity(false); flowLogger.info("Attached new Kafka appender for job " + this.jobId); } If you are sure and expect this should not happen, may be change it to an assert?

Target:
private void attachKafkaAppender(KafkaLog4jAppender appender) { // This should only be called once assert(!kafkaAppender.isPresent()); kafkaAppender = Optional.of(appender); logger.addAppender(kafkaAppender.get()); logger.setAdditivity(false); flowLogger.info("Attached new Kafka appender for job " + this.jobId); }
Source:
private void removeAppender(Optional<Appender> appender) { removeAppender(appender.get()); START appender = Optional.empty(); END } Is this step necessary? I thought it would only affect the reference in this method's scope.

Target:
private void removeAppender(Optional<Appender> appender) { if (appender.isPresent()) { removeAppender(appender.get()); } }
Source:
public static boolean isCronExpressionValid(String cronExpression, DateTimeZone timezone) { if (!CronExpression.isValidExpression(cronExpression)) { return false; } else { CronExpression cronExecutionTime = parseCronExpression(cronExpression, timezone); START if (cronExecutionTime == null || cronExecutionTime.getNextValidTimeAfter(new Date()) == null) { END return false; } return true; } } so we are trying to avoid scheduling flows in the past. right ? Can you please add a small note. nit: else statement is not required.

Target:
public static boolean isCronExpressionValid(String cronExpression, DateTimeZone timezone) { if (!CronExpression.isValidExpression(cronExpression)) { return false; } /* * The below code is aimed at checking some cases that the above code can not identify, * e.g. <0 0 3 ? * * 22> OR <0 0 3 ? * 8>. Under these cases, the below code is able to tell. */ CronExpression cronExecutionTime = parseCronExpression(cronExpression, timezone); if (cronExecutionTime == null || cronExecutionTime.getNextValidTimeAfter(new Date()) == null) { return false; } return true; }
Source:
public boolean isRecurring() { START return period == null && cronExpression == null ? false : true; END } can simply to `return period != null || cronExpression != null;` If you use IntelliJ, it automatically suggests these refactors.

Target:
public boolean isRecurring() { return period != null || cronExpression != null; }
Source:
public ExecutorSelector(List<String> filterList, Map<String,Integer> comparatorList) { START super(null == filterList || filterList.size() == 0 ? null : new ExecutorFilter(filterList), END null == comparatorList || comparatorList.size() == 0 ? null : new ExecutorComparator(comparatorList)); } small thing - filterList.isEmpty() is shorter

Target:
public ExecutorSelector(List<String> filterList, Map<String,Integer> comparatorList) { super(null == filterList || filterList.isEmpty() ? null : new ExecutorFilter(filterList), null == comparatorList || comparatorList.isEmpty() ? null : new ExecutorComparator(comparatorList)); }
Source:
public static ExecutorApiClient getInstance() { START if (null == instance) { END synchronized (ExecutorApiClient.class) { if (null == instance) { instance = new ExecutorApiClient(); } } } return instance; } Since the constructor doesn't do much, another way to initialize an instance of this class w/o doing any synchronization is: private static ExecutorApiClient instance = new ExecutorApiClient();

Target:
public static ExecutorApiClient getInstance() { return instance; }
Source:
protected synchronized void preTrackingEventMethod() { // nothing to finalize value is already updated START } END Why do you have `synchronized` on an empty method?

Target:
protected void preTrackingEventMethod() { // nothing to finalize value is already updated }
Source:
public void addPassMsgs(Set<String> msgs) { START _passMsgs.addAll(msgs); END } This will throw a NPE if msgs is null. You probably want to add a null check. Same thing for the other methods.

Target:
public void addPassMsgs(Set<String> msgs) { if (msgs != null) { _passMsgs.addAll(msgs); } }
Source:
private void insertJVMAargs() { String flowName = node.getParentFlow().getFlowId(); String jobId = node.getId(); String jobJVMArgs = String.format( "-Dazkaban.flowid=%s -Dazkaban.execid=%s -Dazkaban.jobid=%s", flowName, executionId, jobId); String previousJVMArgs = props.get(JavaProcessJob.JVM_PARAMS); if (previousJVMArgs == null) { previousJVMArgs = jobJVMArgs; } else { START previousJVMArgs += " " + jobJVMArgs; END } logger.info("job JVM args: " + previousJVMArgs); props.put(JavaProcessJob.JVM_PARAMS, previousJVMArgs); } I think it would be easier to read if you just combined all this into ``` jobJVMArgs += (previousJVMArgs == null) ? "" : " " + previousJVMArgs ``` and then used jobJVMArgs below instead of previousJVMArgs. I think it'll also think the "ps -ef" output will be easier to read if you put the -Dazkaban... JVM params first, before the previousJVMArgs.

Target:
private void insertJVMAargs() { String flowName = node.getParentFlow().getFlowId(); String jobId = node.getId(); String jobJVMArgs = String.format( "-Dazkaban.flowid=%s -Dazkaban.execid=%s -Dazkaban.jobid=%s", flowName, executionId, jobId); String previousJVMArgs = props.get(JavaProcessJob.JVM_PARAMS); jobJVMArgs += (previousJVMArgs == null) ? "" : " " + previousJVMArgs; logger.info("job JVM args: " + jobJVMArgs); props.put(JavaProcessJob.JVM_PARAMS, jobJVMArgs); }
Source:
public void handleEvent(Event event) { if (event.getType() == Event.Type.FLOW_FINISHED) { FlowRunner flowRunner = (FlowRunner) event.getRunner(); ExecutableFlow flow = flowRunner.getExecutableFlow(); recentlyFinishedFlows.put(flow.getExecutionId(), flow); logger.info("Flow " + flow.getExecutionId() + " is finished. Adding it to recently finished flows list."); runningFlows.remove(flow.getExecutionId()); START submittedFlows.remove(flow.getExecutionId()); END } } This is old code that should be removed. The Key in submittedFlows is no longer an Integer but a Future<?>.

Target:
public void handleEvent(Event event) { if (event.getType() == Event.Type.FLOW_FINISHED) { FlowRunner flowRunner = (FlowRunner) event.getRunner(); ExecutableFlow flow = flowRunner.getExecutableFlow(); recentlyFinishedFlows.put(flow.getExecutionId(), flow); logger.info("Flow " + flow.getExecutionId() + " is finished. Adding it to recently finished flows list."); runningFlows.remove(flow.getExecutionId()); } }