-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
EdgeServerStartupProcessTest.java
209 lines (175 loc) · 9.05 KB
/
EdgeServerStartupProcessTest.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
/*
* Copyright (c) 2002-2016 "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.coreedge;
import org.apache.lucene.document.Field;
import org.junit.Test;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.neo4j.coreedge.catchup.storecopy.LocalDatabase;
import org.neo4j.coreedge.catchup.storecopy.edge.StoreFetcher;
import org.neo4j.coreedge.discovery.ClusterTopology;
import org.neo4j.coreedge.discovery.EdgeTopologyService;
import org.neo4j.coreedge.discovery.TopologyService;
import org.neo4j.coreedge.raft.replication.tx.ConstantTimeRetryStrategy;
import org.neo4j.coreedge.raft.state.CoreStateMachines;
import org.neo4j.coreedge.server.CoreMember;
import org.neo4j.coreedge.server.StoreId;
import org.neo4j.coreedge.server.edge.AlwaysChooseFirstServer;
import org.neo4j.coreedge.server.edge.EdgeServerStartupProcess;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.transaction.state.DataSourceManager;
import org.neo4j.kernel.lifecycle.Lifecycle;
import org.neo4j.logging.NullLogProvider;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.neo4j.helpers.collection.Iterators.asSet;
public class EdgeServerStartupProcessTest
{
@Test
public void startShouldReplaceTheEmptyLocalStoreWithStoreFromCoreServerAndStartPolling() throws Throwable
{
// given
StoreFetcher storeFetcher = mock( StoreFetcher.class );
LocalDatabase localDatabase = mock( LocalDatabase.class );
CoreMember coreMember = new CoreMember( UUID.randomUUID() );
TopologyService hazelcastTopology = mock( TopologyService.class );
ClusterTopology clusterTopology = mock( ClusterTopology.class );
when( hazelcastTopology.currentTopology() ).thenReturn( clusterTopology );
when( clusterTopology.coreMembers() ).thenReturn( asSet( coreMember ) );
when( localDatabase.isEmpty() ).thenReturn( true );
DataSourceManager dataSourceManager = mock( DataSourceManager.class );
Lifecycle txPulling = mock( Lifecycle.class );
EdgeServerStartupProcess edgeServerStartupProcess = new EdgeServerStartupProcess( storeFetcher, localDatabase,
txPulling, dataSourceManager, new AlwaysChooseFirstServer( hazelcastTopology ),
new ConstantTimeRetryStrategy( 1, MILLISECONDS ), NullLogProvider.getInstance(),
mock( EdgeTopologyService.class ), new Config( Collections.emptyMap() ) );
// when
edgeServerStartupProcess.start();
// then
verify( dataSourceManager ).start();
verify( localDatabase ).isEmpty();
verify( localDatabase ).stop();
verify( localDatabase ).copyStoreFrom( coreMember, storeFetcher );
verify( localDatabase ).start();
verify( txPulling ).start();
verifyNoMoreInteractions( localDatabase, dataSourceManager, txPulling );
}
@Test
public void startShouldNotReplaceTheNonEmptyLocalStoreWithStoreFromCoreServerAndStartPolling() throws Throwable
{
// given
StoreFetcher storeFetcher = mock( StoreFetcher.class );
CoreMember coreMember = new CoreMember( UUID.randomUUID() );
LocalDatabase localDatabase = mock( LocalDatabase.class );
when( localDatabase.isEmpty() ).thenReturn( false );
doThrow( IllegalStateException.class ).when( localDatabase ).ensureSameStoreId( coreMember, storeFetcher );
TopologyService hazelcastTopology = mock( TopologyService.class );
ClusterTopology clusterTopology = mock( ClusterTopology.class );
when( hazelcastTopology.currentTopology() ).thenReturn( clusterTopology );
when( clusterTopology.coreMembers() ).thenReturn( asSet( coreMember ) );
DataSourceManager dataSourceManager = mock( DataSourceManager.class );
Lifecycle txPulling = mock( Lifecycle.class );
EdgeServerStartupProcess edgeServerStartupProcess = new EdgeServerStartupProcess( storeFetcher, localDatabase,
txPulling, dataSourceManager, new AlwaysChooseFirstServer( hazelcastTopology ),
new ConstantTimeRetryStrategy( 1, MILLISECONDS ), NullLogProvider.getInstance(),
mock( EdgeTopologyService.class ), new Config( Collections.emptyMap() ) );
// when
try
{
edgeServerStartupProcess.start();
fail( "should have thrown" );
}
catch ( IllegalStateException ex )
{
// expected
}
// then
verify( dataSourceManager ).start();
verify( localDatabase ).isEmpty();
verify( localDatabase ).ensureSameStoreId( coreMember, storeFetcher );
verifyNoMoreInteractions( localDatabase, dataSourceManager );
verifyZeroInteractions( txPulling );
}
@Test
public void startShouldSimplyStartPollingOnNonEmptyDatabaseAndMatchingStoreId() throws Throwable
{
// given
StoreId storeId = new StoreId( 1, 2, 3, 4 );
StoreFetcher storeFetcher = mock( StoreFetcher.class );
when( storeFetcher.storeId( any( CoreMember.class ) ) ).thenReturn( storeId );
LocalDatabase localDatabase = mock( LocalDatabase.class );
when( localDatabase.storeId() ).thenReturn( storeId );
CoreMember coreMember = new CoreMember( UUID.randomUUID() );
TopologyService hazelcastTopology = mock( TopologyService.class );
ClusterTopology clusterTopology = mock( ClusterTopology.class );
when( hazelcastTopology.currentTopology() ).thenReturn( clusterTopology );
when( clusterTopology.coreMembers() ).thenReturn( asSet( coreMember ) );
when( localDatabase.isEmpty() ).thenReturn( false );
DataSourceManager dataSourceManager = mock( DataSourceManager.class );
Lifecycle txPulling = mock( Lifecycle.class );
EdgeServerStartupProcess edgeServerStartupProcess = new EdgeServerStartupProcess( storeFetcher, localDatabase,
txPulling, dataSourceManager, new AlwaysChooseFirstServer( hazelcastTopology ),
new ConstantTimeRetryStrategy( 1, MILLISECONDS ), NullLogProvider.getInstance(),
mock( EdgeTopologyService.class ), new Config( Collections.emptyMap() ) );
// when
edgeServerStartupProcess.start();
// then
verify( localDatabase ).isEmpty();
verify( localDatabase ).ensureSameStoreId( coreMember, storeFetcher );
verify( dataSourceManager ).start();
verify( txPulling ).start();
verifyNoMoreInteractions( localDatabase, dataSourceManager, txPulling );
}
@Test
public void stopShouldStopTheDatabaseAndStopPolling() throws Throwable
{
// given
StoreFetcher storeFetcher = mock( StoreFetcher.class );
LocalDatabase localDatabase = mock( LocalDatabase.class );
CoreMember coreMember = new CoreMember( UUID.randomUUID() );
TopologyService hazelcastTopology = mock( TopologyService.class );
ClusterTopology clusterTopology = mock( ClusterTopology.class );
when( clusterTopology.coreMembers() ).thenReturn( asSet( coreMember ) );
when( hazelcastTopology.currentTopology() ).thenReturn( clusterTopology );
when( localDatabase.isEmpty() ).thenReturn( true );
DataSourceManager dataSourceManager = mock( DataSourceManager.class );
Lifecycle txPulling = mock( Lifecycle.class );
EdgeServerStartupProcess edgeServerStartupProcess = new EdgeServerStartupProcess( storeFetcher, localDatabase,
txPulling, dataSourceManager, new AlwaysChooseFirstServer( hazelcastTopology ),
new ConstantTimeRetryStrategy( 1, MILLISECONDS ), NullLogProvider.getInstance(),
mock( EdgeTopologyService.class ), null );
// when
edgeServerStartupProcess.stop();
// then
verify( txPulling ).stop();
verify( dataSourceManager ).stop();
verifyNoMoreInteractions( txPulling, dataSourceManager );
}
}