From 3d138d3fdd0425598512d33e54a86bf1b9825149 Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Mon, 20 Jun 2022 17:27:56 +0200 Subject: [PATCH] catch error when initializing hazelcast should fix https://github.com/yacy/yacy_search_server/issues/468 --- htroot/api/localpeers.java | 57 +++++++++++++------------ source/net/yacy/search/Switchboard.java | 23 ++++++---- 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/htroot/api/localpeers.java b/htroot/api/localpeers.java index b131245eb..1700be93e 100644 --- a/htroot/api/localpeers.java +++ b/htroot/api/localpeers.java @@ -41,14 +41,14 @@ public class localpeers { public static serverObjects respond(@SuppressWarnings("unused") final RequestHeader header, @SuppressWarnings("unused") final serverObjects post, @SuppressWarnings("unused") final serverSwitch env) { final servletProperties prop = new servletProperties(); int c = 0; - for (String urlstub: Switchboard.getSwitchboard().localcluster_scan) { + for (final String urlstub: Switchboard.getSwitchboard().localcluster_scan) { prop.putJSON("peers_" + c + "_urlstub", urlstub); // a usrlstub is a full url with protocol, host and port up the the path start including first "/" c++; } prop.put("peers", c); try { prop.put("status", systemStatus().toString(2)); - } catch (JSONException e) { + } catch (final JSONException e) { prop.put("status", ""); } // return rewrite properties @@ -58,32 +58,35 @@ public class localpeers { public static JSONObject systemStatus() throws JSONException { // generate json - JSONObject systemStatus = new JSONObject(true); - Memory.status().forEach((k, v) -> {try {systemStatus.put(k, v);} catch (JSONException e) {}}); - JSONArray members = new JSONArray(); - HazelcastInstance hi = Switchboard.getSwitchboard().localcluster_hazelcast; - String uuid = hi.getCluster().getLocalMember().getUuid().toString(); - hi.getMap("status").put(uuid, Memory.status()); - for (Member member: hi.getCluster().getMembers()) { - JSONObject m = new JSONObject(true); - uuid = member.getUuid().toString(); - m.put("uuid", uuid); - m.put("host", member.getAddress().getHost()); - try {m.put("ip", member.getAddress().getInetAddress().getHostAddress());} catch (JSONException | UnknownHostException e) {} - m.put("port", member.getAddress().getPort()); - m.put("isLite", member.isLiteMember()); - m.put("isLocal", member.localMember()); - @SuppressWarnings("unchecked") - Map status = (Map) hi.getMap("status").get(uuid); - m.put("status", status); - members.put(m); + final JSONObject systemStatus = new JSONObject(true); + Memory.status().forEach((k, v) -> {try {systemStatus.put(k, v);} catch (final JSONException e) {}}); + final JSONArray members = new JSONArray(); + final HazelcastInstance hi = Switchboard.getSwitchboard().localcluster_hazelcast; + if (hi != null) { + String uuid = hi.getCluster().getLocalMember().getUuid().toString(); + hi.getMap("status").put(uuid, Memory.status()); + for (final Member member: hi.getCluster().getMembers()) { + final JSONObject m = new JSONObject(true); + uuid = member.getUuid().toString(); + m.put("uuid", uuid); + m.put("host", member.getAddress().getHost()); + try {m.put("ip", member.getAddress().getInetAddress().getHostAddress());} catch (JSONException | UnknownHostException e) {} + m.put("port", member.getAddress().getPort()); + m.put("isLite", member.isLiteMember()); + m.put("isLocal", member.localMember()); + @SuppressWarnings("unchecked") + final + Map status = (Map) hi.getMap("status").get(uuid); + m.put("status", status); + members.put(m); + } + systemStatus.put("hazelcast_cluster_name", hi.getConfig().getClusterName()); + systemStatus.put("hazelcast_instance_name", hi.getConfig().getInstanceName()); + final Collection interfaces = hi.getConfig().getNetworkConfig().getInterfaces().getInterfaces(); + systemStatus.put("hazelcast_interfaces", interfaces); + systemStatus.put("hazelcast_members", members); + systemStatus.put("hazelcast_members_count", members.length()); } - systemStatus.put("hazelcast_cluster_name", hi.getConfig().getClusterName()); - systemStatus.put("hazelcast_instance_name", hi.getConfig().getInstanceName()); - Collection interfaces = hi.getConfig().getNetworkConfig().getInterfaces().getInterfaces(); - systemStatus.put("hazelcast_interfaces", interfaces); - systemStatus.put("hazelcast_members", members); - systemStatus.put("hazelcast_members_count", members.length()); return systemStatus; } diff --git a/source/net/yacy/search/Switchboard.java b/source/net/yacy/search/Switchboard.java index f00fc503e..62e033b34 100644 --- a/source/net/yacy/search/Switchboard.java +++ b/source/net/yacy/search/Switchboard.java @@ -665,9 +665,14 @@ public final class Switchboard extends serverSwitch { join.getMulticastConfig().setEnabled(true); final Config config = new Config().setClusterName("YaCyP2P").setInstanceName("Peer").setNetworkConfig(networkConfig); config.getCPSubsystemConfig().setCPMemberCount(3); - this.localcluster_hazelcast = Hazelcast.newHazelcastInstance(config); - final String uuid = this.localcluster_hazelcast.getCluster().getLocalMember().getUuid().toString(); - this.localcluster_hazelcast.getMap("status").put(uuid, Memory.status()); + try { + this.localcluster_hazelcast = Hazelcast.newHazelcastInstance(config); + final String uuid = this.localcluster_hazelcast.getCluster().getLocalMember().getUuid().toString(); + this.localcluster_hazelcast.getMap("status").put(uuid, Memory.status()); + } catch (final Exception e) { + this.log.warn(e); + this.localcluster_hazelcast = null; + } // load domainList try { @@ -1345,7 +1350,7 @@ public final class Switchboard extends serverSwitch { } @Override - public void setHttpServer(YaCyHttpServer server) { + public void setHttpServer(final YaCyHttpServer server) { super.setHttpServer(server); // finally start jobs which shall be started after start-up @@ -2255,7 +2260,7 @@ public final class Switchboard extends serverSwitch { return moved; } - private boolean processSurrogateJson(File infile, File outfile) { + private boolean processSurrogateJson(final File infile, final File outfile) { // parse a file that can be generated with yacy_grid_parser // see https://github.com/yacy/yacy_grid_parser/blob/master/README.md this.log.info("processing json surrogate " + infile); @@ -3062,7 +3067,7 @@ public final class Switchboard extends serverSwitch { * * @param jobType */ - public void pauseCrawlJob(final String jobType, String cause) { + public void pauseCrawlJob(final String jobType, final String cause) { final Object[] status = this.crawlJobsStatus.get(jobType); synchronized ( status[SwitchboardConstants.CRAWLJOB_SYNC] ) { status[SwitchboardConstants.CRAWLJOB_STATUS] = Boolean.TRUE; @@ -3807,7 +3812,7 @@ public final class Switchboard extends serverSwitch { * @param url * @return null if this was ok. If this failed, return a string with a fail reason */ - public String stackUrl(CrawlProfile profile, DigestURL url) { + public String stackUrl(final CrawlProfile profile, final DigestURL url) { final byte[] handle = ASCII.getBytes(profile.handle()); @@ -4186,11 +4191,11 @@ public final class Switchboard extends serverSwitch { return 1; } - public String encodeDigestAuth(String user, String pw) { + public String encodeDigestAuth(final String user, final String pw) { return "MD5:" + Digest.encodeMD5Hex(user + ":" + sb.getConfig(SwitchboardConstants.ADMIN_REALM,"YaCy") + ":" + pw); } - public String encodeBasicAuth(String user, String pw) { + public String encodeBasicAuth(final String user, final String pw) { return Digest.encodeMD5Hex(user + ":" + pw); }