incubator-hugegraph icon indicating copy to clipboard operation
incubator-hugegraph copied to clipboard

[Question] How to disable legacy master-worker mechanism in multi-server

Open shirleyStorage opened this issue 2 years ago • 50 comments

Problem Type (问题类型)

struct / logic (架构 / 逻辑设计问题)

Before submit

  • [X] 我已经确认现有的 Issues 与 FAQ 中没有相同 / 重复问题 (I have confirmed and searched that there are no similar problems in the historical issue and documents)

Environment (环境信息)

  • Server Version: 1.0.0 (Apache Release Version)
  • Backend: RocksDB x nodes, HDD or SSD
  • OS: xx CPUs, xx G RAM, Ubuntu 2x.x / CentOS 7.x
  • Data Size: xx vertices, xx edges

Your Question (问题描述)

How to disable legacy master-worker mechanism in multi-server

Vertex/Edge example (问题点 / 边数据举例)

No response

Schema [VertexLabel, EdgeLabel, IndexLabel] (元数据结构)

No response

shirleyStorage avatar Sep 22 '24 06:09 shirleyStorage

To disable the legacy master-worker mechanism in a multi-server setup using Apache HugeGraph, you need to set the server.role configuration option to "computer". This can be done in your configuration file as follows:

server.role=computer

This setting ensures that the RoleElectionStateMachine is not initialized, effectively disabling the legacy master-worker mechanism. Here is the relevant configuration option:

public static final ConfigOption<String> SERVER_ROLE =
        new ConfigOption<>(
                "server.role",
                "The role of nodes in the cluster, available types are " +
                "[master, worker, computer]",
                allowValues("master", "worker", "computer"),
                "master"
        );

By setting server.role to "computer", the supportRoleElection will be false, and the role state machine will not be initialized [1][2].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 22 '24 06:09 dosubot[bot]

To disable the legacy master-worker mechanism in a multi-server setup using Apache HugeGraph, you need to set the server.role configuration option to "computer". This can be done in your configuration file as follows:

server.role=computer

This setting ensures that the RoleElectionStateMachine is not initialized, effectively disabling the legacy master-worker mechanism. Here is the relevant configuration option:

public static final ConfigOption<String> SERVER_ROLE =
        new ConfigOption<>(
                "server.role",
                "The role of nodes in the cluster, available types are " +
                "[master, worker, computer]",
                allowValues("master", "worker", "computer"),
                "master"
        );

By setting server.role to "computer", the supportRoleElection will be false, and the role state machine will not be initialized [1][2].

To continue talking to Dosu, mention @dosu.

@dosu Thanks for the direct solution, could u also provide more details about the legacy master-worker role code design & usage (As detailed and systematic as possible)?

imbajin avatar Sep 22 '24 07:09 imbajin

The legacy master-worker role code design in Apache HugeGraph is implemented using an enumeration called NodeRole. This enum defines three roles: MASTER, WORKER, and COMPUTER. Each role is associated with a unique byte code and a name. The NodeRole enum also includes methods to check the role type and retrieve the role's code and name.

Here is the relevant code:

public enum NodeRole implements SerialEnum {

    MASTER(1, "master"),

    WORKER(2, "worker"),

    COMPUTER(3, "computer");

    private final byte code;
    private final String name;

    NodeRole(int code, String name) {
        assert code < 256;
        this.code = (byte) code;
        this.name = name;
    }

    static {
        SerialEnum.register(NodeRole.class);
    }

    @Override
    public byte code() {
        return this.code;
    }

    public String string() {
        return this.name;
    }

    public boolean master() {
        return this == MASTER;
    }

    public boolean worker() {
        return this == WORKER;
    }

    public boolean computer() {
        return this == COMPUTER;
    }
}
  • Roles Defined:

    • MASTER: Represents the master node with code 1 and name "master".
    • WORKER: Represents the worker node with code 2 and name "worker".
    • COMPUTER: Represents the computer node with code 3 and name "computer".
  • Methods:

    • code(): Returns the byte code of the role.
    • string(): Returns the name of the role.
    • master(): Checks if the role is MASTER.
    • worker(): Checks if the role is WORKER.
    • computer(): Checks if the role is COMPUTER.

Additionally, the RoleListener interface in the org.apache.hugegraph.masterelection package defines methods for handling role changes and errors within the system. This interface includes methods such as onAsRoleMaster, onAsRoleWorker, onAsRoleCandidate, unknown, onAsRoleAbdication, and error, each taking a StateMachineContext parameter to manage the state transitions and handle errors.

Here is the relevant code for the RoleListener interface:

package org.apache.hugegraph.masterelection;

public interface RoleListener {

    void onAsRoleMaster(StateMachineContext context);

    void onAsRoleWorker(StateMachineContext context);

    void onAsRoleCandidate(StateMachineContext context);

    void unknown(StateMachineContext context);

    void onAsRoleAbdication(StateMachineContext context);

    void error(StateMachineContext context, Throwable e);
}

The RoleElectionStateMachineTest class provides a test implementation for the role election state machine, demonstrating how roles transition and how the system logs these transitions. The LogEntry class within this test captures the epoch, node, and role for each state change, and the TestConfig class provides configuration details for the state machine.

Here is an excerpt from the RoleElectionStateMachineTest class:

public class RoleElectionStateMachineTest {

    private static class LogEntry {

        private final Integer epoch;
        private final String node;
        private final Role role;

        enum Role {
            master,
            worker,
            candidate,
            abdication,
            unknown
        }

        public LogEntry(Integer epoch, String node, Role role) {
            this.epoch = epoch;
            this.node = node;
            this.role = role;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof LogEntry)) {
                return false;
            }
            LogEntry logEntry = (LogEntry) obj;
            return Objects.equals(this.epoch, logEntry.epoch) &&
                   Objects.equals(this.node, logEntry.node) &&
                   this.role == logEntry.role;
        }

        @Override
        public int hashCode() {
            return Objects.hash(this.epoch, this.node, this.role);
        }

        @Override
        public String toString() {
            return "LogEntry{" +
                   "epoch=" + this.epoch +
                   ", node='" + this.node + '\'' +
                   ", role=" + this.role +
                   '}';
        }
    }

    private static class TestConfig implements Config {

        private final String node;

        public TestConfig(String node) {
            this.node = node;
        }

        @Override
        public String node() {
            return this.node;
        }

        @Override
        public String url() {
            return "http://127.0.0.1:8080";
        }

        @Override
        public int exceedsFailCount() {
            return 2;
        }

        @Override
        public long randomTimeoutMillisecond() {
            return 400;
        }

        @Override
        public long heartBeatIntervalSecond() {
            return 1;
        }

        @Override
        public int masterDeadTimes() {
            return 5;
        }

        @Override
        public long baseTimeoutMillisecond() {
            return 100;
        }
    }

    @Test
    public void testStateMachine() throws InterruptedException {
        final int MAX_COUNT = 200;
        CountDownLatch stop = new CountDownLatch(4);
        List<LogEntry> logRecords = Collections.synchronizedList(new ArrayList<>(MAX_COUNT));
        List<String> masterNodes = Collections.synchronizedList(new ArrayList<>(MAX_COUNT));
        RoleListener callback = new RoleListener() {

            @Override
            public void onAsRoleMaster(StateMachineContext context) {
                Integer epochId = context.epoch();
                String node = context.node();
                logRecords.add(new LogEntry(epochId, node, LogEntry.Role.master));
                if (logRecords.size() > MAX_COUNT) {
                    context.stateMachine().shutdown();
                }
                Utils.println("master node: " + node);
                masterNodes.add(node);
            }

            @Override
            public void onAsRoleWorker(StateMachineContext context) {
                Integer epochId = context.epoch();
                String node = context.node();
                logRecords.add(new LogEntry(epochId, node, LogEntry.Role.worker));
                if (logRecords.size() > MAX_COUNT) {
                    context.stateMachine().shutdown();
                }
            }

            @Override
            public void onAsRoleCandidate(StateMachineContext context) {
                Integer epochId = context.epoch();
                String node = context.node();
                logRecords.add(new LogEntry(epochId, node, LogEntry.Role.candidate));
                if (logRecords.size() > MAX_COUNT) {
                    context.stateMachine().shutdown();
                }
            }
        };
    }
}

This design allows for easy role identification, role-specific logic implementation, and handling of role transitions and errors within the HugeGraph system [1][2][3].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 22 '24 07:09 dosubot[bot]

@dosu What do the three server.role configurations mean?

shirleyStorage avatar Sep 25 '24 15:09 shirleyStorage

The three server.role configurations define the role of nodes in the HugeGraph cluster. The available types are:

  1. master: This node acts as the master in the cluster, typically responsible for coordinating the cluster and managing metadata.
  2. worker: This node handles the actual data processing tasks in the cluster.
  3. computer: This node is used for computational tasks that are distinct from the roles of master and worker nodes.

Here is the relevant configuration option:

public static final ConfigOption<String> SERVER_ROLE =
        new ConfigOption<>(
                "server.role",
                "The role of nodes in the cluster, available types are " +
                "[master, worker, computer]",
                allowValues("master", "worker", "computer"),
                "master"
        );

By setting the server.role configuration, you can define the specific role of each node in your HugeGraph cluster [1][2].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 25 '24 15:09 dosubot[bot]

@dosu How would you start a minimal cluster with each of the three server.role values?

shirleyStorage avatar Sep 25 '24 15:09 shirleyStorage

To start a minimal cluster with each of the three server.role values (master, worker, computer), you need to configure the server.role option in the rest-server.properties configuration file for each server instance. Here is an example of how you can set up the configuration for each role:

  1. Master Node Configuration:

    server.id=server-master
    server.role=master
    restserver.url=http://127.0.0.1:8080
    
  2. Worker Node Configuration:

    server.id=server-worker
    server.role=worker
    restserver.url=http://127.0.0.1:8081
    
  3. Computer Node Configuration:

    server.id=server-computer
    server.role=computer
    restserver.url=http://127.0.0.1:8082
    

Each configuration file should be saved separately and used to start the respective server instance. The server.id and restserver.url values should be unique for each server to avoid conflicts.

Refer to the rest-server.properties configuration file for additional options that may be relevant to your setup, such as ssl.keystore_file, restserver.max_worker_threads, and gremlinserver.url [1][2][3].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 25 '24 15:09 dosubot[bot]

@dosu How to start a single cluster with backend=rocksdb?

shirleyStorage avatar Sep 25 '24 15:09 shirleyStorage

To start a single HugeGraph cluster with backend=rocksdb, follow these steps:

  1. Update hugegraph.properties:

    backend=rocksdb
    serializer=binary
    rocksdb.data_path=.
    rocksdb.wal_path=.
    
  2. Initialize the database (required on first startup or if a new configuration was manually added under 'conf/graphs/'):

    cd *hugegraph-${version}
    bin/init-store.sh
    
  3. Update rest-server.properties:

    restserver.url=http://127.0.0.1:8080
    gremlinserver.url=http://127.0.0.1:8182
    graphs=[hugegraph:conf/hugegraph.properties]
    
  4. Update gremlin-server.yaml:

    host: 127.0.0.1
    port: 8182
    graphs: {
      hugegraph: conf/hugegraph.properties
    }
    
  5. Start the server:

    bin/start-hugegraph.sh
    Starting HugeGraphServer...
    Connecting to HugeGraphServer (http://127.0.0.1:8080/graphs)....OK
    
  6. Verify the server and graphs:

    curl http://127.0.0.1:8080/graphs/
    

    This will list the available graphs, including the one configured with RocksDB.

This will start the HugeGraph server with RocksDB as the backend [1][2][3].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 25 '24 16:09 dosubot[bot]

@dosu What is the recommended jdk version for compiling hugegraph?

shirleyStorage avatar Sep 26 '24 14:09 shirleyStorage

The recommended JDK version for compiling HugeGraph is JDK 1.8.0_111. However, for running HugeGraph-Server, it is recommended to use Java 11 (JDK 11) to ensure better security, especially in production environments or those exposed to the public network [1][2][3].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 26 '24 14:09 dosubot[bot]

@dosu How to run standalone mode in docker after hugegraph is built locally?

shirleyStorage avatar Sep 27 '24 16:09 shirleyStorage

To run HugeGraph in standalone mode using Docker after building it locally, follow these steps:

  1. Build the Docker image: Ensure you are in the directory containing the Dockerfile and run the following command to build the Docker image:

    docker build -t hugegraph-pd .
    
  2. Run the Docker container: Once the image is built, you can run the container in standalone mode using the following command:

    docker run -itd --name=graph -p 8080:8080 hugegraph-pd
    

This command will start the HugeGraph server in a Docker container, exposing port 8080 for access. The docker-entrypoint.sh script will be executed as specified in the Dockerfile.

Alternatively, you can use Docker Compose to manage the deployment:

  1. Create a docker-compose.yaml file with the following content:

    version: '3'
    services:
      graph:
        image: hugegraph-pd
        ports:
          - 8080:8080
    
  2. Run the following command to start the HugeGraph server:

    docker-compose up -d
    

This will start the HugeGraph server and manage other HugeGraph-related instances if needed [1][2].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 27 '24 16:09 dosubot[bot]

@dosu Why use hugegraph-pd instead of hugegraph-server?

shirleyStorage avatar Sep 28 '24 03:09 shirleyStorage

HugeGraph PD is used instead of HugeGraph Server because it is a meta server responsible for service discovery, partition information storage, and node scheduling [1].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 28 '24 04:09 dosubot[bot]

@dosu But I only need hugegraph server to run standalone hugegraph cluster locally?

shirleyStorage avatar Sep 28 '24 04:09 shirleyStorage

To run a standalone HugeGraph server locally, you can use the hugegraph Docker image. Here is the command to start a HugeGraph server with RocksDB as the backend:

docker run -itd --name=graph -p 8080:8080 hugegraph/hugegraph

This command will start the HugeGraph server in the background, and you can access it at http://127.0.0.1:8080 [1].

If you prefer to use a startup script, follow these steps:

  1. Update the hugegraph.properties file with the desired backend configuration. For example, for RocksDB:

    backend=rocksdb
    serializer=binary
    rocksdb.data_path=.
    rocksdb.wal_path=.
    
  2. Initialize the database (required on first startup):

    cd *hugegraph-${version}
    bin/init-store.sh
    
  3. Start the server:

    bin/start-hugegraph.sh
    

This will start the HugeGraph server and connect it to the configured backend storage [1].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 28 '24 04:09 dosubot[bot]

@dosu How do I make a hugegraph/hugegraph docker image from my own hugegraph source code?

shirleyStorage avatar Sep 28 '24 04:09 shirleyStorage

To create a hugegraph/hugegraph Docker image from your own HugeGraph source code, follow these steps:

  1. Ensure you have Docker and Maven installed on your system.

  2. Navigate to the root directory of your HugeGraph source code.

  3. Create a Dockerfile in the root directory with the following content:

    #
    # 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.
    #
    
    # Dockerfile for HugeGraph Server
    # 1st stage: build source code
    FROM maven:3.9.0-eclipse-temurin-11 AS build
    
    COPY . /pkg
    WORKDIR /pkg
    ARG MAVEN_ARGS
    
    RUN mvn package $MAVEN_ARGS -e -B -ntp -Dmaven.test.skip=true -Dmaven.javadoc.skip=true && pwd && ls -l && rm \
        ./hugegraph-server/*.tar.gz && rm ./hugegraph-pd/*.tar.gz && rm ./hugegraph-store/*.tar.gz
    
    # 2nd stage: runtime env
    # Note: ZGC (The Z Garbage Collector) is only supported on ARM-Mac with java > 13 
    FROM openjdk:11-slim
    
    COPY --from=build /pkg/hugegraph-server/apache-hugegraph-server-incubating-*/ /hugegraph-server/
    LABEL maintainer="HugeGraph Docker Maintainers <[email protected]>"
    
    # TODO: use g1gc or zgc as default
    ENV JAVA_OPTS="-XX:+UnlockExperimentalVMOptions -XX:+UseContainerSupport -XX:MaxRAMPercentage=50 -XshowSettings:vm" \
        HUGEGRAPH_HOME="hugegraph-server"
    
    #COPY . /hugegraph/hugegraph-server
    WORKDIR /hugegraph-server/
    
    # 1. Install environment and init HugeGraph Sever
    RUN set -x \
        && apt-get -q update \
        && apt-get -q install -y --no-install-recommends --no-install-suggests \
           dumb-init \
           procps \
           curl \
           lsof \
           vim \
           cron \
        && apt-get clean \
        && rm -rf /var/lib/apt/lists/* \
        && service cron start \
        && pwd && cd /hugegraph-server/ \
        && sed -i "s/^restserver.url.*$/restserver.url=http:\/\/0.0.0.0:8080/g" ./conf/rest-server.properties
    
    # 2. Init docker script
    COPY hugegraph-server/hugegraph-dist/docker/scripts/remote-connect.groovy ./scripts
    COPY hugegraph-server/hugegraph-dist/docker/scripts/detect-storage.groovy ./scripts
    COPY hugegraph-server/hugegraph-dist/docker/docker-entrypoint.sh .
    RUN chmod 755 ./docker-entrypoint.sh
    
    EXPOSE 8080
    VOLUME /hugegraph-server
    
    ENTRYPOINT ["/usr/bin/dumb-init", "--"]
    CMD ["./docker-entrypoint.sh"]
    
  4. Build the Docker image using the following command:

    docker build -t hugegraph/hugegraph .
    

This command will create a Docker image named hugegraph/hugegraph from your HugeGraph source code [1][2].

To continue talking to Dosu, mention @dosu.

dosubot[bot] avatar Sep 28 '24 04:09 dosubot[bot]

@dosu How do I make a hugegraph/hugegraph docker image from my own hugegraph source code?only for backend=rocksdb and start a standalone cluster

shirleyStorage avatar Sep 28 '24 07:09 shirleyStorage