Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvements on background/foreground indexing #930

Merged
merged 6 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/org/ecocean/Encounter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4192,11 +4192,18 @@ public static int[] opensearchSyncIndex(Shepherd myShepherd)
public static int[] opensearchSyncIndex(Shepherd myShepherd, int stopAfter)
throws IOException {
int[] rtn = new int[2];

if (OpenSearch.indexingActive()) {
System.out.println("Encounter.opensearchSyncIndex() skipped due to indexingActive()");
rtn[0] = -1;
rtn[1] = -1;
return rtn;
}
OpenSearch.setActiveIndexingBackground();
String indexName = "encounter";
OpenSearch os = new OpenSearch();
List<List<String> > changes = os.resolveVersions(getAllVersions(myShepherd),
os.getAllVersions(indexName));

if (changes.size() != 2) throw new IOException("invalid resolveVersions results");
List<String> needIndexing = changes.get(0);
List<String> needRemoval = changes.get(1);
Expand Down Expand Up @@ -4227,6 +4234,7 @@ public static int[] opensearchSyncIndex(Shepherd myShepherd, int stopAfter)
ct++;
}
System.out.println("Encounter.opensearchSyncIndex() finished needRemoval");
OpenSearch.unsetActiveIndexingBackground();
return rtn;
}

Expand Down
78 changes: 78 additions & 0 deletions src/main/java/org/ecocean/OpenSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class OpenSearch {
public static int BACKGROUND_SLICE_SIZE = (Integer)getConfigurationValue("backgroundSliceSize",
2500);
public static String QUERY_STORAGE_DIR = "/tmp"; // FIXME
static String ACTIVE_TYPE_FOREGROUND = "opensearch_indexing_foreground";
static String ACTIVE_TYPE_BACKGROUND = "opensearch_indexing_background";

private int pitRetry = 0;

Expand Down Expand Up @@ -574,6 +576,82 @@ public Long getIndexTimestamp(Shepherd myShepherd, String indexName) {
return SystemValue.getLong(myShepherd, INDEX_TIMESTAMP_PREFIX + indexName);
}

public static boolean indexingActive() {
return indexingActiveBackground() || indexingActiveForeground();
}

public static boolean indexingActiveForeground() {
return getActive(ACTIVE_TYPE_FOREGROUND);
}

public static void setActiveIndexingForeground() {
setActive(ACTIVE_TYPE_FOREGROUND);
}

public static void unsetActiveIndexingForeground() {
unsetActive(ACTIVE_TYPE_FOREGROUND);
}

public static boolean indexingActiveBackground() {
return getActive(ACTIVE_TYPE_BACKGROUND);
}

public static void setActiveIndexingBackground() {
setActive(ACTIVE_TYPE_BACKGROUND);
}

public static void unsetActiveIndexingBackground() {
unsetActive(ACTIVE_TYPE_BACKGROUND);
}

static void setActive(String type) {
// we want our own shepherd as the main shepherd may not persist this til later
Shepherd myShepherd = new Shepherd("context0");

myShepherd.setAction("OpenSearch.setActive");
myShepherd.beginDBTransaction();
try {
SystemValue.set(myShepherd, type, true);
myShepherd.commitDBTransaction();
myShepherd.closeDBTransaction();
} catch (Exception ex) {
ex.printStackTrace();
myShepherd.rollbackAndClose();
}
}

static void unsetActive(String type) {
Shepherd myShepherd = new Shepherd("context0");

myShepherd.setAction("OpenSearch.unsetActive");
myShepherd.beginDBTransaction();
try {
SystemValue.set(myShepherd, type, false);
myShepherd.commitDBTransaction();
myShepherd.closeDBTransaction();
} catch (Exception ex) {
ex.printStackTrace();
myShepherd.rollbackAndClose();
}
}

// TODO probably should get in some sort of expire/stale check here
static boolean getActive(String type) {
Boolean active = false;
Shepherd myShepherd = new Shepherd("context0");

myShepherd.setAction("OpenSearch.getActive");
myShepherd.beginDBTransaction();
try {
active = SystemValue.getBoolean(myShepherd, type);
} catch (Exception ex) {
ex.printStackTrace();
}
myShepherd.rollbackAndClose();
if (active == null) return false;
return active;
}

public static JSONObject querySanitize(JSONObject query, User user) {
if ((query == null) || (user == null)) return query;
JSONObject newQuery = new JSONObject(query.toString());
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/org/ecocean/StartupWildbook.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public static void ensureProfilePhotoKeywordExists(Shepherd myShepherd) {
// these get run with each tomcat startup/shutdown, if web.xml is configured accordingly. see, e.g. https://stackoverflow.com/a/785802
public void contextInitialized(ServletContextEvent sce) {
ServletContext sContext = sce.getServletContext();
String context = "context0";
String context = "context0";

System.out.println(new org.joda.time.DateTime() + " ### StartupWildbook initialized for: " +
servletContextInfo(sContext));
Expand All @@ -171,11 +171,13 @@ public void contextInitialized(ServletContextEvent sce) {
createMatchGraph();
}
// TODO: set strategy for the following (genericize starting "all" consumers, make configurable, move to WildbookIAM.startup, move to plugins, or other)
startIAQueues(context);
startIAQueues(context);
TwitterBot.startServices(context);
MetricsBot.startServices(context);
AcmIdBot.startServices(context);
AnnotationLite.startup(sContext, context);
OpenSearch.unsetActiveIndexingBackground(); // since tomcat is just starting, these reset to false
OpenSearch.unsetActiveIndexingForeground();
OpenSearch.backgroundStartup(context);

try {
Expand Down Expand Up @@ -307,7 +309,7 @@ private static void startWildbookScheduledTaskThread(String context) {

public void contextDestroyed(ServletContextEvent sce) {
ServletContext sContext = sce.getServletContext();
String context = "context0";
String context = "context0";

System.out.println("* StartupWildbook destroyed called for: " +
servletContextInfo(sContext));
Expand All @@ -327,7 +329,7 @@ public static void createMatchGraph() {

public static boolean skipInit(ServletContextEvent sce, String extra) {
ServletContext sc = sce.getServletContext();

/* WARNING! this bad hackery to try to work around "double deployment" ... yuck!
see: https://octopus.com/blog/defining-tomcat-context-paths
*/
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/ecocean/SystemValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public static SystemValue set(Shepherd myShepherd, String key, JSONObject val) {
return _set(myShepherd, key, "JSONObject", val);
}

public static SystemValue set(Shepherd myShepherd, String key, Boolean val) {
return _set(myShepherd, key, "Boolean", val);
}

public static JSONObject getValue(Shepherd myShepherd, String key) {
SystemValue sv = load(myShepherd, key);

Expand Down Expand Up @@ -150,6 +154,19 @@ public static String getString(Shepherd myShepherd, String key) {
}
}

public static Boolean getBoolean(Shepherd myShepherd, String key) {
JSONObject v = getValue(myShepherd, key);

if (v == null) return null;
if (v.isNull("value")) return null;
try {
return v.getBoolean("value");
} catch (JSONException ex) {
System.out.println("WARNING: parse error on " + v.toString() + ": " + ex.toString());
return null;
}
}

public static JSONObject getJSONObject(Shepherd myShepherd, String key) {
JSONObject v = getValue(myShepherd, key);

Expand Down
11 changes: 11 additions & 0 deletions src/main/webapp/appadmin/opensearchSync.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ OpenSearch os = new OpenSearch();

if (resetIndex && os.existsIndex("encounter")) {
os.deleteIndex("encounter");
OpenSearch.unsetActiveIndexingForeground();
OpenSearch.unsetActiveIndexingBackground();
out.println("<p>deleted encounter index</p>");
}

if (OpenSearch.indexingActive()) {
out.println("<P>bailing due to active indexing: fore=<b>" + OpenSearch.indexingActiveForeground() + "</b>, back=<b>" + OpenSearch.indexingActiveBackground() + "</b></p>");
System.out.println("opensearchSync.jsp bailed due to other active indexing");
return;
}

OpenSearch.setActiveIndexingForeground();

if (!os.existsIndex("encounter")) {
Encounter enc = new Encounter();
enc.opensearchCreateIndex();
Expand Down Expand Up @@ -60,6 +70,7 @@ if (forceNum > 0) {

myShepherd.rollbackAndClose();

OpenSearch.unsetActiveIndexingForeground();
os.deleteAllPits();
System.out.println("opensearchSync.jsp finished");

Expand Down
Loading