Skip to content

Commit

Permalink
Performance improvement by using multithreading.
Browse files Browse the repository at this point in the history
  • Loading branch information
chkp-rdecker authored Aug 15, 2018
1 parent f996b5a commit 92c99a6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ enum ShowPackageConfiguration {

INSTANCE;

private static final String TOOL_VERSION = "v2.0.1";
private static final String TOOL_VERSION = "v2.0.2";
private static final String TAR_SUFFIX = ".tar.gz";
private static final String LOG_SUFFIX = ".elg";
private static final String PREFIX = "show_package-";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
import java.util.concurrent.*;
import java.util.logging.Handler;

/**
Expand All @@ -56,6 +57,8 @@ public class ShowPackageTool {
private static ApiLoginResponse loginResponse;
private static JSONObject allTypes = null;

private static final int NUMBER_OF_EXECUTORS = 2;

private static final String TYPE = "type";
private static final String UNDEFINED = "undefined";

Expand Down Expand Up @@ -749,22 +752,42 @@ private static boolean showRulebase(Layer layer, String packageName, String comm
}

if (totalObjects > 0) {
int offset = 0;
final ExecutorService executorService = Executors.newFixedThreadPool(NUMBER_OF_EXECUTORS);
List<Callable<ApiResponse>> tasks = new ArrayList<>(totalObjects / limit);

List<JSONObject> chunks = new ArrayList<>(totalObjects / limit);
int offset = 0;
while (offset < totalObjects) {
JSONObject payload = new JSONObject(payloadTemplate);
payload.put("offset", offset);
payload.put("limit", limit);
chunks.add(payload);
tasks.add(new ApiCallTask(command, payload));

offset += limit;
}

try {
configuration.getLogger().debug("Command [" + command + "] uid " + payloadTemplate.get("uid")
+ " : Starting execution of " + tasks.size() + " tasks (with " + NUMBER_OF_EXECUTORS + " executor(s))");

final List<Future<ApiResponse>> futures = executorService.invokeAll(tasks);
executorService.shutdown();

boolean serviceTerminatedSuccessfully = executorService.awaitTermination(3, TimeUnit.HOURS);

if (!serviceTerminatedSuccessfully) {
configuration.getLogger().severe("Failed to run show rulebase (" + layer.getName() + "). Timeout after 3 hours.");
configuration.getLogger().debug("Following the error, creating an empty html file for layer: '"
+ layer.getName() + "'");
writeRulebase(layer.getName(), packageName, rulebaseType, layer.getDomain(), inlineLayers, true);
return false;
}

configuration.getLogger().debug("Command [" + command + "] uid " + payloadTemplate.get("uid") + " : Finished execution of " + tasks.size() + " tasks");

JSONArray rulebases = new JSONArray();
for (JSONObject chunk : chunks) {
res = client.apiCall(loginResponse, command, chunk);

for (Future<ApiResponse> f : futures) {
res = f.get();

JSONArray jsonArrayOfObjectDictionary = (JSONArray)res.getPayload().get("objects-dictionary");
addObjectsInfoIntoCollections(jsonArrayOfObjectDictionary);
Expand All @@ -786,7 +809,6 @@ private static boolean showRulebase(Layer layer, String packageName, String comm
// firstNew is deleted
currentRulebase.remove(0);
}

}

rulebases.addAll(currentRulebase);
Expand All @@ -806,7 +828,7 @@ private static boolean showRulebase(Layer layer, String packageName, String comm

inlineLayers.addAll(addRulebase(rulebases, types, rulebaseType));
}
catch (ApiClientException e) {
catch (InterruptedException | ExecutionException e) {
handleException(e, "Failed to run show rulebase (" + layer.getName() + ")");
configuration.getLogger().debug("Following the error, creating an empty html file for layer: '"
+ layer.getName() + "'");
Expand All @@ -815,10 +837,10 @@ private static boolean showRulebase(Layer layer, String packageName, String comm
}
}


configuration.getLogger().debug("Found " + totalObjects + " rules in : '" + layer.getName() + "'");
configuration.getLogger().debug("Found " + inlineLayers.size() + " inline layer(s)");
configuration.getLogger().debug("Creating html file for layer: '" + layer.getName() + "'");

boolean writeRulebaseResult = writeRulebase(layer.getName(), packageName, rulebaseType,
layer.getDomain(), inlineLayers, false);

Expand All @@ -840,6 +862,7 @@ private static boolean showRulebase(Layer layer, String packageName, String comm
configuration.getLogger().warning("Failed to create inline-layer, name: '" + inlineLayer.getName() + "'");
}
}

configuration.getLogger().info("Done handling rulebase '" + layer.getName() + "'");

return writeRulebaseResult;
Expand Down Expand Up @@ -1623,7 +1646,37 @@ private static void freeResources(){
handle.close();
}
}
}

private static class ApiCallTask implements Callable<ApiResponse> {

private JSONObject payload;
private String command;

ApiCallTask(String command, JSONObject payload)
{
this.payload = payload;
this.command = command;
}

@Override
public ApiResponse call()
{
ApiResponse res;
try {
res = client.apiCall(loginResponse, command, payload);
}
catch (Exception e) {
res = null;
}

if (res == null || !res.isSuccess()) {
res = null;
}

String log = "Command [" + command + "] uid " + payload.get("uid") + " limit " + payload.get("limit") + " offset " + payload.get("offset") + " ";
configuration.getLogger().debug(log + (res == null ? "FAILED" : "SUCCESSFUL"));

return res;
}
}
}

0 comments on commit 92c99a6

Please sign in to comment.