summaryrefslogtreecommitdiff
path: root/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java
blob: 99991c51f4062abd86cc163fe994af24beefc32c (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
 * Copyright 2012 Kjetil Ørbekk <kjetil.orbekk@gmail.com>
 *
 * Licensed 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 com.orbekk.paxos;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.protobuf.RpcCallback;
import com.google.protobuf.RpcController;
import com.orbekk.same.Services.Paxos;
import com.orbekk.same.Services.PaxosRequest;
import com.orbekk.same.Services.PaxosResponse;

/**
 * This class better be thread-safe.
 */
public class PaxosServiceImpl {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private int highestPromise = 0;
    private int highestAcceptedValue = 0;
    private String tag = "";
    private Paxos service = new ProtobufPaxosServiceImpl();
    
    private class ProtobufPaxosServiceImpl extends Paxos {
        @Override
        public void propose(RpcController controller, PaxosRequest request,
                RpcCallback<PaxosResponse> done) {
            logger.info("propose({}). Highest promise: {}, Highest accepted: {}",
                    new Object[]{request, highestPromise, highestAcceptedValue});
            String clientUrl = request.getClient().getLocation();
            int proposalNumber = request.getProposalNumber();
            int response = 
                    PaxosServiceImpl.this.propose(clientUrl, proposalNumber);
            PaxosResponse result = PaxosResponse.newBuilder()
                    .setResult(response)
                    .build();
            done.run(result);
        }

        @Override
        public void acceptRequest(RpcController controller,
                PaxosRequest request, RpcCallback<PaxosResponse> done) {
            logger.info("acceptRequest({}). Highest promise: {}, Highest accepted: {}",
                    new Object[]{request, highestPromise, highestAcceptedValue});
            String clientUrl = request.getClient().getLocation();
            int proposalNumber = request.getProposalNumber();
            int response = 
                    PaxosServiceImpl.this.acceptRequest(clientUrl, proposalNumber);
            PaxosResponse result = PaxosResponse.newBuilder()
                    .setResult(response)
                    .build();
            done.run(result);
        }
        
    }
    
    public PaxosServiceImpl(String tag) {
        this.tag = tag;
    }
    
    public Paxos getService() {
        return service;
    }

    private synchronized int propose(String clientUrl,
            int proposalNumber) {
        if (proposalNumber > highestPromise) {
            logger.info(tag + "propose({}, {}) = accepted",
                    new Object[]{clientUrl, proposalNumber});
            highestPromise = proposalNumber;
            return highestPromise;
        } else {
            logger.info(tag + "propose({}, {}) = rejected " +
                    "(promised: {})",
                    new Object[]{clientUrl, proposalNumber,
                    highestPromise});
            return -highestPromise;
        }
    }

    private synchronized int acceptRequest(String clientUrl,
            int proposalNumber) {
        if (proposalNumber == highestPromise) {
            logger.info(tag + "acceptRequest({}, {}) = accepted",
                    new Object[]{clientUrl, proposalNumber});
            highestAcceptedValue = proposalNumber;
            return highestAcceptedValue;
        } else {
            logger.info(tag + "acceptRequest({}, {}) = rejected " +
                    "(promise={})",
                    new Object[]{clientUrl, proposalNumber,
                    highestPromise});
            return -highestPromise;
        }
    }
}