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

[Bug] pd make the RaftEngine constructor private #2655

Open
1 task done
haohao0103 opened this issue Sep 4, 2024 · 3 comments
Open
1 task done

[Bug] pd make the RaftEngine constructor private #2655

haohao0103 opened this issue Sep 4, 2024 · 3 comments
Labels
bug Something isn't working pd PD module raft

Comments

@haohao0103
Copy link
Contributor

Bug Type (问题类型)

logic (逻辑设计问题)

Before submit

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

Environment (环境信息)

Server Version: 1.5

Expected & Actual behavior (期望与实际表现)

The RaftEngine is intended to be a global singleton, but the constructor is not private.

public RaftEngine() {
        this.stateMachine = new RaftStateMachine();
    }

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

No response

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

No response

@haohao0103 haohao0103 added the bug Something isn't working label Sep 4, 2024
@dosubot dosubot bot added pd PD module raft labels Sep 4, 2024
Copy link

dosubot bot commented Sep 4, 2024

The RaftEngine constructor is not private because it is used to initialize the singleton instance within the class itself. The singleton pattern is implemented using a private static final instance of RaftEngine and a public static method getInstance() to provide access to this instance. The constructor is package-private (default access) to restrict instantiation from outside the package, ensuring that the singleton instance is the only instance used.

Here is the relevant code:

public class RaftEngine {

    private static final RaftEngine INSTANCE = new RaftEngine();
    private final RaftStateMachine stateMachine;
    private PDConfig.Raft config;
    private RaftGroupService raftGroupService;
    private RpcServer rpcServer;
    private Node raftNode;
    private RaftRpcClient raftRpcClient;

    RaftEngine() {
        this.stateMachine = new RaftStateMachine();
    }

    public static RaftEngine getInstance() {
        return INSTANCE;
    }

    // Other methods...
}

The constructor RaftEngine() is package-private, which means it can only be accessed within the same package. This design ensures that the RaftEngine can only be instantiated once, and only the getInstance() method can be used to access the singleton instance [1].

To continue talking to Dosu, mention @dosu.

@haohao0103
Copy link
Contributor Author

@dosu
The RaftEngine constructor is actually public, so I think this breaks the design of the singleton pattern

Copy link

dosubot bot commented Sep 5, 2024

You are correct. To adhere to the singleton pattern, the RaftEngine constructor should be made private. This ensures that only one instance of the class is created and prevents the creation of additional instances.

Here is the modified code with the RaftEngine constructor made private:

public class RaftEngine {

    private static final RaftEngine INSTANCE = new RaftEngine();
    private final RaftStateMachine stateMachine;
    private PDConfig.Raft config;
    private RaftGroupService raftGroupService;
    private RpcServer rpcServer;
    private Node raftNode;
    private RaftRpcClient raftRpcClient;

    private RaftEngine() {
        this.stateMachine = new RaftStateMachine();
    }

    public static RaftEngine getInstance() {
        return INSTANCE;
    }

    // Rest of the class implementation
}

This change ensures that the RaftEngine class follows the singleton pattern correctly [1].

To continue talking to Dosu, mention @dosu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pd PD module raft
Projects
Status: In progress
Development

No branches or pull requests

1 participant