summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/paxos/MasterProposer.java
blob: 3a451d0424c2542a2c056cec3ba0fbd80eb2edca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.orbekk.paxos;

import java.util.ArrayList;
import java.util.List;

import com.orbekk.same.ConnectionManager;

public class MasterProposer {
    private String myUrl;
    private List<String> paxosUrls = new ArrayList<String>();
    private ConnectionManager connections;
    
    public MasterProposer(String clientUrl, List<String> paxosUrls,
            ConnectionManager connections) {
        this.myUrl = clientUrl;
        this.paxosUrls = paxosUrls;
        this.connections = connections;
    }
    
    private boolean internalPropose(int roundId, int proposalNumber) {
        int promises = 0;
        for (String url : paxosUrls) {
            PaxosService paxos = connections.getPaxos(url);
            boolean success = paxos.propose(myUrl, roundId, proposalNumber);
            if (success) {
                promises += 1;
            }
        }
        return promises > paxosUrls.size() / 2;
    }
    
    private boolean internalAcceptRequest(int roundId, int proposalNumber) {
        int accepts = 0;
        for (String url : paxosUrls) {
            PaxosService paxos = connections.getPaxos(url);
            boolean success = paxos.acceptRequest(myUrl, roundId, proposalNumber);
            if (success) {
                accepts += 1;
            }
        }
        return accepts > paxosUrls.size() / 2;        
    }

    public boolean propose(int roundId, int proposalNumber) {
        boolean success = false;
        success = internalPropose(roundId, proposalNumber);
        if (success) {
            success = internalAcceptRequest(roundId, proposalNumber);
        }
        if (success) {
            return true;
        } else {
            return false;
        }
    }
}