-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
DefaultUdcInformationCollector.java
431 lines (380 loc) · 13.9 KB
/
DefaultUdcInformationCollector.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
* Copyright (c) 2002-2018 "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 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.ext.udc.impl;
import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.regex.Pattern;
import org.neo4j.ext.udc.UdcSettings;
import org.neo4j.graphdb.ResourceIterator;
import org.neo4j.graphdb.config.Setting;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.kernel.NeoStoreDataSource;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.core.StartupStatistics;
import org.neo4j.kernel.impl.store.id.IdGeneratorFactory;
import org.neo4j.kernel.impl.store.id.IdType;
import org.neo4j.kernel.impl.transaction.state.DataSourceManager;
import org.neo4j.io.os.OsBeanUtil;
import org.neo4j.storageengine.api.StoreFileMetadata;
import org.neo4j.udc.UsageData;
import org.neo4j.udc.UsageDataKeys;
import static org.neo4j.ext.udc.UdcConstants.CLUSTER_HASH;
import static org.neo4j.ext.udc.UdcConstants.DATABASE_MODE;
import static org.neo4j.ext.udc.UdcConstants.DISTRIBUTION;
import static org.neo4j.ext.udc.UdcConstants.EDITION;
import static org.neo4j.ext.udc.UdcConstants.FEATURES;
import static org.neo4j.ext.udc.UdcConstants.HEAP_SIZE;
import static org.neo4j.ext.udc.UdcConstants.ID;
import static org.neo4j.ext.udc.UdcConstants.LABEL_IDS_IN_USE;
import static org.neo4j.ext.udc.UdcConstants.MAC;
import static org.neo4j.ext.udc.UdcConstants.NODE_IDS_IN_USE;
import static org.neo4j.ext.udc.UdcConstants.NUM_PROCESSORS;
import static org.neo4j.ext.udc.UdcConstants.OS_PROPERTY_PREFIX;
import static org.neo4j.ext.udc.UdcConstants.PROPERTY_IDS_IN_USE;
import static org.neo4j.ext.udc.UdcConstants.REGISTRATION;
import static org.neo4j.ext.udc.UdcConstants.RELATIONSHIP_IDS_IN_USE;
import static org.neo4j.ext.udc.UdcConstants.REVISION;
import static org.neo4j.ext.udc.UdcConstants.SERVER_ID;
import static org.neo4j.ext.udc.UdcConstants.SOURCE;
import static org.neo4j.ext.udc.UdcConstants.STORE_SIZE;
import static org.neo4j.ext.udc.UdcConstants.TAGS;
import static org.neo4j.ext.udc.UdcConstants.TOTAL_MEMORY;
import static org.neo4j.ext.udc.UdcConstants.UDC_PROPERTY_PREFIX;
import static org.neo4j.ext.udc.UdcConstants.UNKNOWN_DIST;
import static org.neo4j.ext.udc.UdcConstants.USER_AGENTS;
import static org.neo4j.ext.udc.UdcConstants.VERSION;
public class DefaultUdcInformationCollector implements UdcInformationCollector
{
private final Config config;
private final UsageData usageData;
private final IdGeneratorFactory idGeneratorFactory;
private String storeId;
private boolean crashPing;
private NeoStoreDataSource neoStoreDataSource;
public DefaultUdcInformationCollector( Config config, DataSourceManager xadsm,
IdGeneratorFactory idGeneratorFactory, StartupStatistics startupStats, UsageData usageData )
{
this.config = config;
this.usageData = usageData;
this.idGeneratorFactory = idGeneratorFactory;
final StartupStatistics startupStatistics = startupStats;
if ( xadsm != null )
{
xadsm.addListener( new DataSourceManager.Listener()
{
@Override
public void registered( NeoStoreDataSource ds )
{
crashPing = startupStatistics.numberOfRecoveredTransactions() > 0;
storeId = Long.toHexString( ds.getStoreId().getRandomId() );
neoStoreDataSource = ds;
}
@Override
public void unregistered( NeoStoreDataSource ds )
{
crashPing = false;
storeId = null;
neoStoreDataSource = null;
}
} );
}
}
public static String filterVersionForUDC( String version )
{
if ( !version.contains( "+" ) )
{
return version;
}
return version.substring( 0, version.indexOf( "+" ) );
}
@Override
public Map<String,String> getUdcParams()
{
String classPath = getClassPath();
Map<String,String> udcFields = new HashMap<>();
add( udcFields, ID, storeId );
add( udcFields, VERSION, filterVersionForUDC( usageData.get( UsageDataKeys.version ) ) );
add( udcFields, REVISION, filterVersionForUDC( usageData.get( UsageDataKeys.revision ) ) );
add( udcFields, EDITION, usageData.get( UsageDataKeys.edition ).name().toLowerCase() );
add( udcFields, SOURCE, config.get( UdcSettings.udc_source ) );
add( udcFields, REGISTRATION, config.get( UdcSettings.udc_registration_key ) );
add( udcFields, DATABASE_MODE, usageData.get( UsageDataKeys.operationalMode ).name() );
add( udcFields, SERVER_ID, usageData.get( UsageDataKeys.serverId ) );
add( udcFields, USER_AGENTS, toCommaString( usageData.get( UsageDataKeys.clientNames ) ) );
add( udcFields, TAGS, determineTags( jarNamesForTags, classPath ) );
add( udcFields, CLUSTER_HASH, determineClusterNameHash() );
add( udcFields, MAC, determineMacAddress() );
add( udcFields, DISTRIBUTION, determineOsDistribution() );
add( udcFields, NUM_PROCESSORS, determineNumberOfProcessors() );
add( udcFields, TOTAL_MEMORY, determineTotalMemory() );
add( udcFields, HEAP_SIZE, determineHeapSize() );
add( udcFields, NODE_IDS_IN_USE, determineNodesIdsInUse() );
add( udcFields, RELATIONSHIP_IDS_IN_USE, determineRelationshipIdsInUse() );
add( udcFields, LABEL_IDS_IN_USE, determineLabelIdsInUse() );
add( udcFields, PROPERTY_IDS_IN_USE, determinePropertyIdsInUse() );
add( udcFields, FEATURES, usageData.get( UsageDataKeys.features ).asHex() );
addStoreFileSizes( udcFields );
udcFields.putAll( determineSystemProperties() );
return udcFields;
}
/**
* Register store file sizes, sans transaction logs
*/
private void addStoreFileSizes( Map<String,String> udcFields )
{
if ( neoStoreDataSource == null )
{
return;
}
try ( ResourceIterator<StoreFileMetadata> files = neoStoreDataSource.listStoreFiles( false ) )
{
addStoreFileSizes( udcFields, files );
}
catch ( IOException | NullPointerException ignored )
{
// we just didn't report store file sizes this time around
}
}
private void addStoreFileSizes( Map<String,String> udcFields, ResourceIterator<StoreFileMetadata> files )
{
long size = 0;
while ( files.hasNext() )
{
File file = files.next().file();
size += file.length();
}
add( udcFields, STORE_SIZE, size );
}
private String determineOsDistribution()
{
if ( System.getProperties().getProperty( "os.name", "" ).equals( "Linux" ) )
{
return searchForPackageSystems();
}
else
{
return UNKNOWN_DIST;
}
}
static String searchForPackageSystems()
{
try
{
if ( new File( "/bin/rpm" ).exists() )
{
return "rpm";
}
if ( new File( "/usr/bin/dpkg" ).exists() )
{
return "dpkg";
}
}
catch ( Exception e )
{
// ignore
}
return UNKNOWN_DIST;
}
private Integer determineClusterNameHash()
{
try
{
Class<?> settings = Class.forName( "org.neo4j.cluster.ClusterSettings" );
Setting setting = (Setting) settings.getField( "cluster_name" ).get( null );
Object name = config.get( setting );
return name != null ? Math.abs( name.hashCode() % Integer.MAX_VALUE ) : null;
}
catch ( Exception e )
{
return null;
}
}
private final Map<String,String> jarNamesForTags =
MapUtil.stringMap( "spring-", "spring", "(javax.ejb|ejb-jar)", "ejb",
"(weblogic|glassfish|websphere|jboss)", "appserver", "openshift", "openshift", "cloudfoundry",
"cloudfoundry", "(junit|testng)", "test", "jruby", "ruby", "clojure", "clojure", "jython", "python",
"groovy", "groovy", "(tomcat|jetty)", "web", "spring-data-neo4j", "sdn" );
private String determineTags( Map<String,String> jarNamesForTags, String classPath )
{
StringBuilder result = new StringBuilder();
for ( Map.Entry<String,String> entry : jarNamesForTags.entrySet() )
{
final Pattern pattern = Pattern.compile( entry.getKey() );
if ( pattern.matcher( classPath ).find() )
{
result.append( "," ).append( entry.getValue() );
}
}
if ( result.length() == 0 )
{
return null;
}
return result.substring( 1 );
}
private String getClassPath()
{
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
return runtime.getClassPath();
}
private String determineMacAddress()
{
String formattedMac = "0";
try
{
InetAddress address = InetAddress.getLocalHost();
NetworkInterface ni = NetworkInterface.getByInetAddress( address );
if ( ni != null )
{
byte[] mac = ni.getHardwareAddress();
if ( mac != null )
{
StringBuilder sb = new StringBuilder( mac.length * 2 );
Formatter formatter = new Formatter( sb );
for ( byte b : mac )
{
formatter.format( "%02x", b );
}
formattedMac = sb.toString();
}
}
}
catch ( Throwable t )
{
//
}
return formattedMac;
}
private int determineNumberOfProcessors()
{
return Runtime.getRuntime().availableProcessors();
}
private long determineTotalMemory()
{
return OsBeanUtil.getTotalPhysicalMemory();
}
private long determineHeapSize()
{
return ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
}
private long determineNodesIdsInUse()
{
return getNumberOfIdsInUse( IdType.NODE );
}
private long determineLabelIdsInUse()
{
return getNumberOfIdsInUse( IdType.LABEL_TOKEN );
}
private long determinePropertyIdsInUse()
{
return getNumberOfIdsInUse( IdType.PROPERTY );
}
private long determineRelationshipIdsInUse()
{
return getNumberOfIdsInUse( IdType.RELATIONSHIP );
}
private long getNumberOfIdsInUse( IdType type )
{
return idGeneratorFactory.get( type ).getNumberOfIdsInUse();
}
private String toCommaString( Object values )
{
StringBuilder result = new StringBuilder();
if ( values instanceof Iterable )
{
for ( Object agent : (Iterable) values )
{
if ( agent == null )
{
continue;
}
if ( result.length() > 0 )
{
result.append( "," );
}
result.append( agent );
}
}
else
{
result.append( values );
}
return result.toString();
}
private void add( Map<String,String> udcFields, String name, Object value )
{
if ( value == null )
{
return;
}
String str = value.toString().trim();
if ( str.isEmpty() )
{
return;
}
udcFields.put( name, str );
}
private String removeUdcPrefix( String propertyName )
{
if ( propertyName.startsWith( UDC_PROPERTY_PREFIX ) )
{
return propertyName.substring( UDC_PROPERTY_PREFIX.length() + 1 );
}
return propertyName;
}
private String sanitizeUdcProperty( String propertyValue )
{
return propertyValue.replace( ' ', '_' );
}
private Map<String,String> determineSystemProperties()
{
Map<String,String> relevantSysProps = new HashMap<>();
Properties sysProps = System.getProperties();
Enumeration<?> sysPropsNames = sysProps.propertyNames();
while ( sysPropsNames.hasMoreElements() )
{
String sysPropName = (String) sysPropsNames.nextElement();
if ( sysPropName.startsWith( UDC_PROPERTY_PREFIX ) || sysPropName.startsWith( OS_PROPERTY_PREFIX ) )
{
String propertyValue = sysProps.getProperty( sysPropName );
relevantSysProps.put( removeUdcPrefix( sysPropName ), sanitizeUdcProperty( propertyValue ) );
}
}
return relevantSysProps;
}
@Override
public String getStoreId()
{
return storeId;
}
@Override
public boolean getCrashPing()
{
return crashPing;
}
}