-
Notifications
You must be signed in to change notification settings - Fork 157
[Issue #1215] Implement NodeService, Retina Node Heartbeat, and Consistent Hashing #1216
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
pixels-common/src/main/java/io/pixelsdb/pixels/common/node/NodeService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| /* | ||
| * Copyright 2025 PixelsDB. | ||
| * | ||
| * This file is part of Pixels. | ||
| * | ||
| * Pixels is free software: you can redistribute it and/or modify | ||
| * it under the terms of the Affero GNU General Public License as | ||
| * published by the Free Software Foundation, either version 3 of | ||
| * the License, or (at your option) any later version. | ||
| * | ||
| * Pixels is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * Affero GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the Affero GNU General Public | ||
| * License along with Pixels. If not, see | ||
| * <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package io.pixelsdb.pixels.common.node; | ||
|
|
||
| import com.google.protobuf.Empty; | ||
| import io.grpc.ManagedChannel; | ||
| import io.grpc.ManagedChannelBuilder; | ||
| import io.pixelsdb.pixels.common.server.HostAddress; | ||
| import io.pixelsdb.pixels.common.utils.ConfigFactory; | ||
| import io.pixelsdb.pixels.common.utils.ShutdownHookManager; | ||
| import io.pixelsdb.pixels.daemon.NodeProto; | ||
| import io.pixelsdb.pixels.daemon.NodeServiceGrpc; | ||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class NodeService | ||
| { | ||
| private static final Logger logger = LogManager.getLogger(NodeService.class); | ||
|
|
||
| private static final NodeService defaultInstance; | ||
| private static final Map<HostAddress, NodeService> otherInstances = new ConcurrentHashMap<>(); | ||
|
|
||
| static | ||
| { | ||
| String host = ConfigFactory.Instance().getProperty("node.server.host"); | ||
| int port = Integer.parseInt(ConfigFactory.Instance().getProperty("node.server.port")); | ||
|
|
||
| defaultInstance = new NodeService(host, port); | ||
|
|
||
| ShutdownHookManager.Instance().registerShutdownHook(NodeService.class, false, () -> | ||
| { | ||
| try | ||
| { | ||
| defaultInstance.shutdown(); | ||
| for (NodeService client : otherInstances.values()) | ||
| { | ||
| client.shutdown(); | ||
| } | ||
| otherInstances.clear(); | ||
| } catch (InterruptedException e) | ||
| { | ||
| logger.error("Failed to shutdown NodeService", e); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private final ManagedChannel channel; | ||
| private final NodeServiceGrpc.NodeServiceBlockingStub stub; | ||
| private volatile boolean isShutDown; | ||
| private NodeService(String host, int port) | ||
| { | ||
| assert host != null; | ||
| assert port > 0 && port <= 65535; | ||
|
|
||
| this.channel = ManagedChannelBuilder.forAddress(host, port) | ||
| .usePlaintext() | ||
| .build(); | ||
|
|
||
| this.stub = NodeServiceGrpc.newBlockingStub(channel); | ||
| this.isShutDown = false; | ||
| } | ||
|
|
||
| public static NodeService Instance() | ||
| { | ||
| return defaultInstance; | ||
| } | ||
|
|
||
| public static synchronized NodeService CreateInstance(String host, int port) | ||
| { | ||
| HostAddress address = HostAddress.fromParts(host, port); | ||
| NodeService client = otherInstances.get(address); | ||
| if (client != null) | ||
| { | ||
| return client; | ||
| } | ||
| client = new NodeService(host, port); | ||
| otherInstances.put(address, client); | ||
| return client; | ||
| } | ||
|
|
||
| private synchronized void shutdown() throws InterruptedException | ||
| { | ||
| if (!this.isShutDown) | ||
| { | ||
| this.channel.shutdown().awaitTermination(5, TimeUnit.SECONDS); | ||
| this.isShutDown = true; | ||
| } | ||
| } | ||
|
|
||
| public List<NodeProto.NodeInfo> getRetinaList() | ||
| { | ||
| NodeProto.GetRetinaListResponse resp; | ||
|
|
||
| try | ||
| { | ||
| resp = stub.getRetinaList(Empty.getDefaultInstance()); | ||
| } catch (Exception e) | ||
| { | ||
| logger.error("Failed to call GetRetinaList", e); | ||
| throw e; | ||
| } | ||
|
|
||
| if (resp.getErrorCode() != 0) | ||
| { | ||
| logger.error("GetRetinaList returned error code {}", resp.getErrorCode()); | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| return resp.getNodesList(); | ||
| } | ||
|
|
||
| public NodeProto.NodeInfo getRetinaByBucket(int bucketId) | ||
| { | ||
| NodeProto.GetRetinaByBucketRequest req = | ||
| NodeProto.GetRetinaByBucketRequest.newBuilder() | ||
| .setBucket(bucketId) | ||
| .build(); | ||
|
|
||
| NodeProto.GetRetinaByBucketResponse resp; | ||
|
|
||
| try | ||
| { | ||
| resp = stub.getRetinaByBucket(req); | ||
| } catch (Exception e) | ||
| { | ||
| logger.error("Failed to call GetRetinaByBucket", e); | ||
| throw e; | ||
| } | ||
|
|
||
| if (resp.getErrorCode() != 0) | ||
| { | ||
| logger.error("GetRetinaByBucket returned error={}", resp.getErrorCode()); | ||
| return null; // or throw exception | ||
| } | ||
|
|
||
| return resp.getNode(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -68,6 +68,13 @@ heartbeat.lease.ttl.seconds=20 | |||||||
| # heartbeat period must be larger than 0 | ||||||||
| heartbeat.period.seconds=10 | ||||||||
|
|
||||||||
| ###### pixels-node settings ###### | ||||||||
| node.server.port=18891 | ||||||||
| node.server.host=localhost | ||||||||
| # number of virtual nodes per physical node (used in consistent hashing) | ||||||||
| node.virtual.num=16 | ||||||||
|
||||||||
| node.virtual.num=16 | |
| node.virtual.num=16 | |
| # the total number of hash buckets in the consistent hash ring |
52 changes: 52 additions & 0 deletions
52
pixels-common/src/test/java/io/pixelsdb/pixels/common/node/TestNodeService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * Copyright 2025 PixelsDB. | ||
|
bianhq marked this conversation as resolved.
|
||
| * | ||
| * This file is part of Pixels. | ||
| * | ||
| * Pixels is free software: you can redistribute it and/or modify | ||
| * it under the terms of the Affero GNU General Public License as | ||
| * published by the Free Software Foundation, either version 3 of | ||
| * the License, or (at your option) any later version. | ||
| * | ||
| * Pixels is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * Affero GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the Affero GNU General Public | ||
| * License along with Pixels. If not, see | ||
| * <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package io.pixelsdb.pixels.common.node; | ||
|
|
||
| import io.pixelsdb.pixels.daemon.NodeProto; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class TestNodeService | ||
| { | ||
| private static final Logger logger = LoggerFactory.getLogger(TestNodeService.class); | ||
| @Test | ||
| public void testGetRetinaList() | ||
| { | ||
| NodeService nodeService = NodeService.Instance(); | ||
| List<NodeProto.NodeInfo> retinaList = nodeService.getRetinaList(); | ||
| logger.debug("Retina List Size: {}", retinaList.size()); | ||
| for(NodeProto.NodeInfo nodeInfo : retinaList) | ||
| { | ||
| logger.debug(nodeInfo.toString()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetRetinaByBucketId() | ||
| { | ||
| NodeService nodeService = NodeService.Instance(); | ||
| NodeProto.NodeInfo retinaByBucket = nodeService.getRetinaByBucket(1); | ||
| logger.info("Retina By Bucket: {}", retinaByBucket); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.