-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LearnerContextImpl.java
241 lines (215 loc) · 9.47 KB
/
LearnerContextImpl.java
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright (c) 2002-2015 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.context;
import org.neo4j.cluster.protocol.atomicbroadcast.AtomicBroadcastSerializer;
import org.neo4j.cluster.protocol.atomicbroadcast.ObjectInputStreamFactory;
import org.neo4j.cluster.protocol.atomicbroadcast.ObjectOutputStreamFactory;
import org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.AcceptorInstanceStore;
import org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.InstanceId;
import org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerContext;
import org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.LearnerState;
import org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.PaxosInstance;
import org.neo4j.cluster.protocol.atomicbroadcast.multipaxos.PaxosInstanceStore;
import org.neo4j.cluster.protocol.heartbeat.HeartbeatContext;
import org.neo4j.cluster.timeout.Timeouts;
import org.neo4j.kernel.impl.util.CappedLogger;
import org.neo4j.kernel.logging.Logging;
class LearnerContextImpl
extends AbstractContextImpl
implements LearnerContext
{
// LearnerContext
private long lastDeliveredInstanceId = -1;
private long lastLearnedInstanceId = -1;
/** To minimize logging, keep track of the latest learn miss, only log when it changes. */
private final CappedLogger learnMissLogger;
private final HeartbeatContext heartbeatContext;
private final AcceptorInstanceStore instanceStore;
private final ObjectInputStreamFactory objectInputStreamFactory;
private final ObjectOutputStreamFactory objectOutputStreamFactory;
private final PaxosInstanceStore paxosInstances;
LearnerContextImpl( org.neo4j.cluster.InstanceId me, CommonContextState commonState,
Logging logging,
Timeouts timeouts, PaxosInstanceStore paxosInstances,
AcceptorInstanceStore instanceStore,
ObjectInputStreamFactory objectInputStreamFactory,
ObjectOutputStreamFactory objectOutputStreamFactory,
HeartbeatContext heartbeatContext )
{
super( me, commonState, logging, timeouts );
this.heartbeatContext = heartbeatContext;
this.instanceStore = instanceStore;
this.objectInputStreamFactory = objectInputStreamFactory;
this.objectOutputStreamFactory = objectOutputStreamFactory;
this.paxosInstances = paxosInstances;
this.learnMissLogger = new CappedLogger( getLogger( LearnerState.class ) ).setDuplicateFilterEnabled( true );
}
private LearnerContextImpl( org.neo4j.cluster.InstanceId me, CommonContextState commonState, Logging logging,
Timeouts timeouts, long lastDeliveredInstanceId, long lastLearnedInstanceId,
HeartbeatContext heartbeatContext,
AcceptorInstanceStore instanceStore, ObjectInputStreamFactory objectInputStreamFactory,
ObjectOutputStreamFactory objectOutputStreamFactory, PaxosInstanceStore paxosInstances )
{
super( me, commonState, logging, timeouts );
this.lastDeliveredInstanceId = lastDeliveredInstanceId;
this.lastLearnedInstanceId = lastLearnedInstanceId;
this.heartbeatContext = heartbeatContext;
this.instanceStore = instanceStore;
this.objectInputStreamFactory = objectInputStreamFactory;
this.objectOutputStreamFactory = objectOutputStreamFactory;
this.paxosInstances = paxosInstances;
this.learnMissLogger = new CappedLogger( getLogger( LearnerState.class ) ).setDuplicateFilterEnabled( true );
}
@Override
public long getLastDeliveredInstanceId()
{
return lastDeliveredInstanceId;
}
@Override
public void setLastDeliveredInstanceId( long lastDeliveredInstanceId )
{
this.lastDeliveredInstanceId = lastDeliveredInstanceId;
instanceStore.lastDelivered( new InstanceId( lastDeliveredInstanceId ) );
}
@Override
public long getLastLearnedInstanceId()
{
return lastLearnedInstanceId;
}
@Override
public long getLastKnownLearnedInstanceInCluster()
{
return commonState.lastKnownLearnedInstanceInCluster();
}
@Override
public void setLastKnownLearnedInstanceInCluster( long lastKnownLearnedInstanceInCluster,
org.neo4j.cluster.InstanceId instanceId )
{
commonState.setLastKnownLearnedInstanceInCluster( lastKnownLearnedInstanceInCluster, instanceId );
}
@Override
public org.neo4j.cluster.InstanceId getLastKnownAliveUpToDateInstance()
{
return commonState.getLastKnownAliveUpToDateInstance();
}
@Override
public void learnedInstanceId( long instanceId )
{
this.lastLearnedInstanceId = Math.max( lastLearnedInstanceId, instanceId );
if ( lastLearnedInstanceId > commonState.lastKnownLearnedInstanceInCluster() )
{
commonState.setLastKnownLearnedInstanceInCluster( lastLearnedInstanceId, null );
}
}
@Override
public boolean hasDeliveredAllKnownInstances()
{
return lastDeliveredInstanceId == commonState.lastKnownLearnedInstanceInCluster();
}
@Override
public void leave()
{
lastDeliveredInstanceId = -1;
lastLearnedInstanceId = -1;
commonState.setLastKnownLearnedInstanceInCluster( -1, null );
}
@Override
public PaxosInstance getPaxosInstance( InstanceId instanceId )
{
return paxosInstances.getPaxosInstance( instanceId );
}
@Override
public AtomicBroadcastSerializer newSerializer()
{
return new AtomicBroadcastSerializer( objectInputStreamFactory, objectOutputStreamFactory );
}
@Override
public Iterable<org.neo4j.cluster.InstanceId> getAlive()
{
return heartbeatContext.getAlive();
}
@Override
public void setNextInstanceId( long id )
{
commonState.setNextInstanceId( id );
}
@Override
public void notifyLearnMiss( InstanceId instanceId )
{
learnMissLogger.warn( "Did not have learned value for Paxos instance " + instanceId + ". " +
"This generally indicates that this instance has missed too many cluster events and is " +
"failing to catch up. If this error does not resolve soon it may become necessary to " +
"restart this cluster member so normal operation can resume.", null );
}
public LearnerContextImpl snapshot( CommonContextState commonStateSnapshot, Logging logging, Timeouts timeouts,
PaxosInstanceStore paxosInstancesSnapshot, AcceptorInstanceStore instanceStore,
ObjectInputStreamFactory objectInputStreamFactory, ObjectOutputStreamFactory
objectOutputStreamFactory, HeartbeatContextImpl snapshotHeartbeatContext )
{
return new LearnerContextImpl( me, commonStateSnapshot, logging, timeouts, lastDeliveredInstanceId,
lastLearnedInstanceId, snapshotHeartbeatContext, instanceStore, objectInputStreamFactory,
objectOutputStreamFactory, paxosInstancesSnapshot );
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
LearnerContextImpl that = (LearnerContextImpl) o;
if ( lastDeliveredInstanceId != that.lastDeliveredInstanceId )
{
return false;
}
if ( lastLearnedInstanceId != that.lastLearnedInstanceId )
{
return false;
}
if ( heartbeatContext != null ? !heartbeatContext.equals( that.heartbeatContext ) : that.heartbeatContext !=
null )
{
return false;
}
if ( instanceStore != null ? !instanceStore.equals( that.instanceStore ) : that.instanceStore != null )
{
return false;
}
if ( paxosInstances != null ? !paxosInstances.equals( that.paxosInstances ) : that.paxosInstances != null )
{
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = (int) (lastDeliveredInstanceId ^ (lastDeliveredInstanceId >>> 32));
result = 31 * result + (int) (lastLearnedInstanceId ^ (lastLearnedInstanceId >>> 32));
result = 31 * result + (heartbeatContext != null ? heartbeatContext.hashCode() : 0);
result = 31 * result + (instanceStore != null ? instanceStore.hashCode() : 0);
result = 31 * result + (paxosInstances != null ? paxosInstances.hashCode() : 0);
return result;
}
}