-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ClusterMetrics.java
162 lines (141 loc) · 5.57 KB
/
ClusterMetrics.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
/*
* 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.metrics.source;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.MetricRegistry;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicLong;
import org.neo4j.function.Predicate;
import org.neo4j.function.Predicates;
import org.neo4j.graphdb.DependencyResolver;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.ha.SlaveUpdatePuller;
import org.neo4j.kernel.ha.cluster.member.ClusterMembers;
import org.neo4j.kernel.impl.logging.LogService;
import org.neo4j.kernel.lifecycle.LifecycleAdapter;
import org.neo4j.kernel.monitoring.Monitors;
import org.neo4j.metrics.MetricsSettings;
import static com.codahale.metrics.MetricRegistry.name;
import static org.neo4j.kernel.ha.cluster.modeswitch.HighAvailabilityModeSwitcher.MASTER;
import static org.neo4j.kernel.ha.cluster.modeswitch.HighAvailabilityModeSwitcher.UNKNOWN;
public class ClusterMetrics extends LifecycleAdapter
{
private static final String NAME_PREFIX = "neo4j.cluster";
private static final String SLAVE_PULL_UPDATES = name( NAME_PREFIX, "slave_pull_updates" );
private static final String SLAVE_PULL_UPDATE_UP_TO_TX = name( NAME_PREFIX, "slave_pull_update_up_to_tx" );
static final String IS_MASTER = name( NAME_PREFIX, "is_master" );
static final String IS_AVAILABLE = name( NAME_PREFIX, "is_available" );
private final Config config;
private final Monitors monitors;
private final MetricRegistry registry;
private final DependencyResolver dependencyResolver;
private final LogService logService;
private final SlaveUpdatePullerMonitor monitor = new SlaveUpdatePullerMonitor();
private ClusterMembers clusterMembers = null;
public ClusterMetrics( Config config, Monitors monitors, MetricRegistry registry,
DependencyResolver dependencyResolver, LogService logService )
{
this.config = config;
this.monitors = monitors;
this.registry = registry;
this.dependencyResolver = dependencyResolver;
this.logService = logService;
}
@Override
public void start() throws Throwable
{
if ( config.get( MetricsSettings.neoClusterEnabled ) && resolveClusterMembersDependencyOrLogWarning() )
{
monitors.addMonitorListener( monitor );
registry.register( IS_MASTER, new RoleGauge( Predicates.equalTo( MASTER ) ) );
registry.register( IS_AVAILABLE, new RoleGauge( Predicates.not( Predicates.equalTo( UNKNOWN ) ) ) );
registry.register( SLAVE_PULL_UPDATES, new Gauge<Long>()
{
@Override
public Long getValue()
{
return monitor.events.get();
}
} );
registry.register( SLAVE_PULL_UPDATE_UP_TO_TX, new Gauge<Long>()
{
@Override
public Long getValue()
{
return monitor.lastAppliedTxId;
}
} );
}
}
private boolean resolveClusterMembersDependencyOrLogWarning()
{
try
{
clusterMembers = dependencyResolver.resolveDependency( ClusterMembers.class );
return true;
}
catch ( IllegalArgumentException e )
{
logService.getUserLog( getClass() ).warn( "Cluster metrics was enabled but the graph database" +
"is not in HA mode." );
return false;
}
}
@Override
public void stop() throws IOException
{
if ( config.get( MetricsSettings.neoClusterEnabled ) && (clusterMembers != null) )
{
registry.remove( SLAVE_PULL_UPDATES );
registry.remove( SLAVE_PULL_UPDATE_UP_TO_TX );
registry.remove( IS_MASTER );
registry.remove( IS_AVAILABLE );
monitors.removeMonitorListener( monitor );
}
}
private static class SlaveUpdatePullerMonitor implements SlaveUpdatePuller.Monitor
{
private AtomicLong events = new AtomicLong();
private volatile long lastAppliedTxId;
@Override
public void pulledUpdates( long lastAppliedTxId )
{
events.incrementAndGet();
this.lastAppliedTxId = lastAppliedTxId;
}
}
private class RoleGauge implements Gauge<Integer>
{
private Predicate<String> rolePredicate;
public RoleGauge( Predicate<String> rolePredicate )
{
this.rolePredicate = rolePredicate;
}
public Integer getValue()
{
int value = 0;
if ( clusterMembers != null )
{
value = rolePredicate.test( clusterMembers.getCurrentMemberRole() ) ? 1 : 0;
}
return value;
}
}
}