You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by GitBox <gi...@apache.org> on 2022/07/20 11:21:46 UTC

[GitHub] [sling-org-apache-sling-discovery-oak] mreutegg commented on a diff in pull request #7: SLING-11450 : avoid suppressing instances that were seen previously

mreutegg commented on code in PR #7:
URL: https://github.com/apache/sling-org-apache-sling-discovery-oak/pull/7#discussion_r925486388


##########
src/main/java/org/apache/sling/discovery/oak/cluster/ClusterReader.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.discovery.oak.cluster;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.sling.api.resource.PersistenceException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.discovery.commons.providers.spi.base.IdMapService;
+import org.apache.sling.discovery.oak.Config;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Helper class to detect instances that are only partially started.
+ * Partially in this context means that they have updated their
+ * Oak lease but have not written the required data on the Sling 
+ * level (which includes the idmap, the synctoken and the leaderElectionid).
+ */
+public class ClusterReader {
+
+    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    private final IdMapService idMapService;
+
+    private final ResourceResolver resourceResolver;
+
+    private final Config config;
+
+    private final Map<Integer, InstanceInfo> seenLocalInstances;
+
+    public ClusterReader(ResourceResolver resourceResolver, Config config, IdMapService idMapService, Map<Integer, InstanceInfo> seenLocalInstances) {
+        this.resourceResolver = resourceResolver;
+        this.config = config;
+        this.idMapService = idMapService;
+        this.seenLocalInstances = seenLocalInstances == null ? new HashMap<>() : seenLocalInstances;
+    }
+
+    private long readSyncToken(String slingId) {
+        if (slingId == null) {
+            throw new IllegalStateException("slingId must not be null");
+        }
+        final Resource syncTokenNode = resourceResolver
+                .getResource(config.getSyncTokenPath());
+        if (syncTokenNode == null) {
+            return -1;
+        }
+        final ValueMap resourceMap = syncTokenNode.adaptTo(ValueMap.class);
+        if (resourceMap == null) {
+            return -1;
+        }
+        final String syncTokenStr = resourceMap.get(slingId, String.class);
+        if (syncTokenStr == null) {
+            return -1;
+        }
+        try {
+            return Long.parseLong(syncTokenStr);
+        } catch (NumberFormatException nfe) {
+            logger.warn(
+                    "readSyncToken: unparsable (non long) syncToken: " + syncTokenStr);

Review Comment:
   String concatenation can be avoided with format specifier.
   ```suggestion
                       "readSyncToken: unparsable (non long) syncToken: {}", syncTokenStr);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org