-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ResourceTypes.java
209 lines (184 loc) · 7.86 KB
/
ResourceTypes.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-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.locking;
import org.eclipse.collections.api.map.primitive.MutableIntObjectMap;
import org.eclipse.collections.impl.map.mutable.primitive.IntObjectHashMap;
import org.neo4j.hashing.HashFunction;
import org.neo4j.helpers.Strings;
import org.neo4j.internal.kernel.api.IndexQuery;
import org.neo4j.kernel.impl.util.concurrent.LockWaitStrategies;
import org.neo4j.storageengine.api.lock.ResourceType;
import org.neo4j.storageengine.api.lock.WaitStrategy;
import org.neo4j.util.FeatureToggles;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.Values;
public enum ResourceTypes implements ResourceType
{
NODE( 0, LockWaitStrategies.INCREMENTAL_BACKOFF ),
RELATIONSHIP( 1, LockWaitStrategies.INCREMENTAL_BACKOFF ),
GRAPH_PROPS( 2, LockWaitStrategies.INCREMENTAL_BACKOFF ),
// SCHEMA resource type had typeId 3 - skip it to avoid resource types conflicts
INDEX_ENTRY( 4, LockWaitStrategies.INCREMENTAL_BACKOFF ),
EXPLICIT_INDEX( 5, LockWaitStrategies.INCREMENTAL_BACKOFF ),
LABEL( 6, LockWaitStrategies.INCREMENTAL_BACKOFF ),
RELATIONSHIP_TYPE( 7, LockWaitStrategies.INCREMENTAL_BACKOFF ),
/**
* Resources ids of the type SPECIAL_SINGLETON each represent a distinct (singleton) resource that is used for a
* special purpose in the database kernel. The individual ids otherwise have nothing in common.
* <p>
* Each resource is given by a SINGLETON_* constant in this file.
*/
SPECIAL_SINGLETON( 8, LockWaitStrategies.INCREMENTAL_BACKOFF );
private static final boolean useStrongHashing =
FeatureToggles.flag( ResourceTypes.class, "useStrongHashing", false );
private static final MutableIntObjectMap<ResourceType> idToType = new IntObjectHashMap<>();
private static final HashFunction indexEntryHash_2_2_0 = HashFunction.xorShift32();
private static final HashFunction indexEntryHash_4_x = HashFunction.incrementalXXH64();
/**
* The SINGLETON_TOKEN_CREATE resource constant is used to coordinate the creation of new tokens, with locks taken
* by "any-token" indexes when they are created or dropped. These indexes need to lock all tokens of a particular
* type. To do this, they need to ensure that no new tokens are created concurrently with their critical section.
* Token creates take a shared lock on this resource, because token creation is internally synchronised and can be
* allowed to happen in parallel. Meanwhile, dropping or creating "any token" indexes will take exclusive locks on
* this resource to prevent new tokens from being allocated.
*/
public static final int SINGLETON_TOKEN_CREATE = 1;
/**
* Nodes can be created without any labels, but would still be indexed by "any-token" node indexes. To coordinate
* schema locking between these two cases, we use this special singleton lock.
*/
public static final int SINGLETON_UNLABELLED_NODE = 2;
static
{
for ( ResourceTypes resourceTypes : ResourceTypes.values() )
{
idToType.put( resourceTypes.typeId, resourceTypes );
}
}
private final int typeId;
private final WaitStrategy waitStrategy;
ResourceTypes( int typeId, WaitStrategy waitStrategy )
{
this.typeId = typeId;
this.waitStrategy = waitStrategy;
}
@Override
public int typeId()
{
return typeId;
}
@Override
public WaitStrategy waitStrategy()
{
return waitStrategy;
}
/**
* The index entry hashing method used for entries in explicit indexes.
*/
public static long explicitIndexResourceId( String name, String key )
{
return (long) name.hashCode() << 32 | key.hashCode();
}
/**
* This is the schema index entry hashing method used since 2.2.0 and onwards.
* <p>
* Use the {@link ResourceTypes#useStrongHashing} feature toggle to use a stronger hash function, which will become
* the default in a future release. <strong>Note</strong> that changing this hash function is effectively a
* clustering protocol change in HA setups. Causal cluster setups are unaffected because followers do not take any
* locks on the cluster leader.
*/
public static long indexEntryResourceId( long labelId, IndexQuery.ExactPredicate... predicates )
{
if ( !useStrongHashing )
{
// Default
return indexEntryResourceId_2_2_0( labelId, predicates );
}
else
{
// Opt-in
return indexEntryResourceId_4_x( labelId, predicates );
}
}
static long indexEntryResourceId_2_2_0( long labelId, IndexQuery.ExactPredicate[] predicates )
{
return indexEntryResourceId_2_2_0( labelId, predicates, 0 );
}
private static long indexEntryResourceId_2_2_0( long labelId, IndexQuery.ExactPredicate[] predicates, int i )
{
int propertyKeyId = predicates[i].propertyKeyId();
Value value = predicates[i].value();
// Note:
// It is important that single-property indexes only hash with this particular call; no additional hashing!
long hash = indexEntryResourceId_2_2_0( labelId, propertyKeyId, stringOf( value ) );
i++;
if ( i < predicates.length )
{
hash = hash( hash + indexEntryResourceId_2_2_0( labelId, predicates, i ) );
}
return hash;
}
private static long indexEntryResourceId_2_2_0( long labelId, long propertyKeyId, String propertyValue )
{
long hob = hash( labelId + hash( propertyKeyId ) );
hob <<= 32;
return hob + propertyValue.hashCode();
}
private static String stringOf( Value value )
{
if ( value != null && value != Values.NO_VALUE )
{
return Strings.prettyPrint( value.asObject() );
}
return "";
}
private static int hash( long value )
{
return indexEntryHash_2_2_0.hashSingleValueToInt( value );
}
public static long graphPropertyResource()
{
return 0L;
}
public static ResourceType fromId( int typeId )
{
return idToType.get( typeId );
}
/**
* This is a stronger, full 64-bit hashing method for schema index entries that we should use by default in a
* future release, where we will also upgrade the HA protocol version. Currently this is indicated by the "4_x"
* name suffix, but any version where the HA protocol version changes anyway would be just as good an opportunity.
*
* @see HashFunction#incrementalXXH64()
*/
static long indexEntryResourceId_4_x( long labelId, IndexQuery.ExactPredicate[] predicates )
{
long hash = indexEntryHash_4_x.initialise( 0x0123456789abcdefL );
hash = indexEntryHash_4_x.update( hash, labelId );
for ( IndexQuery.ExactPredicate predicate : predicates )
{
int propertyKeyId = predicate.propertyKeyId();
hash = indexEntryHash_4_x.update( hash, propertyKeyId );
Value value = predicate.value();
hash = value.updateHash( indexEntryHash_4_x, hash );
}
return indexEntryHash_4_x.finalise( hash );
}
}