1
+ /*
2
+ * Copyright 2014-2019 Real Logic Ltd.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
1
16
package io .aeron .samples ;
2
17
3
18
import io .aeron .CncFileDescriptor ;
6
21
import org .agrona .DirectBuffer ;
7
22
import org .agrona .IoUtil ;
8
23
import org .agrona .SemanticVersion ;
9
- import org .agrona .concurrent .ringbuffer .ManyToOneRingBuffer ;
24
+ import org .agrona .concurrent .UnsafeBuffer ;
25
+ import org .agrona .concurrent .ringbuffer .RingBufferDescriptor ;
10
26
import org .agrona .concurrent .status .CountersReader ;
11
27
12
28
import java .io .File ;
16
32
import static io .aeron .samples .SamplesUtil .mapExistingFileReadOnly ;
17
33
18
34
/**
19
- * A utility class for interpreting the cnc file .
35
+ * Reader for Aeron CnC file represented by {@link CncFileDescriptor} which can be used for observability .
20
36
*/
21
37
public final class CncFileReader implements AutoCloseable
22
38
{
23
- private final MappedByteBuffer cncByteBuffer ;
39
+ private boolean isClosed = false ;
24
40
private final int cncVersion ;
25
- private final CountersReader countersReader ;
26
- private final ManyToOneRingBuffer toDriverBuffer ;
27
41
private final String cncSemanticVersion ;
42
+ private final MappedByteBuffer cncByteBuffer ;
43
+ private final CountersReader countersReader ;
44
+ private final UnsafeBuffer toDriverBuffer ;
28
45
29
46
private CncFileReader (final MappedByteBuffer cncByteBuffer )
30
47
{
@@ -46,8 +63,7 @@ private CncFileReader(final MappedByteBuffer cncByteBuffer)
46
63
this .cncVersion = cncVersion ;
47
64
this .cncSemanticVersion = SemanticVersion .toString (cncVersion );
48
65
49
- this .toDriverBuffer = new ManyToOneRingBuffer (
50
- CncFileDescriptor .createToDriverBuffer (cncByteBuffer , cncMetaDataBuffer ));
66
+ this .toDriverBuffer = CncFileDescriptor .createToDriverBuffer (cncByteBuffer , cncMetaDataBuffer );
51
67
52
68
this .countersReader = new CountersReader (
53
69
createCountersMetaDataBuffer (cncByteBuffer , cncMetaDataBuffer ),
@@ -64,6 +80,7 @@ public static CncFileReader map()
64
80
{
65
81
final File cncFile = CommonContext .newDefaultCncFile ();
66
82
final MappedByteBuffer cncByteBuffer = mapExistingFileReadOnly (cncFile );
83
+
67
84
return new CncFileReader (cncByteBuffer );
68
85
}
69
86
@@ -98,28 +115,34 @@ public CountersReader countersReader()
98
115
}
99
116
100
117
/**
101
- * Get the timestamp (ms) of the last driver heartbeat.
118
+ * Get the epoch timestamp (ms) of the last driver heartbeat.
102
119
*
103
- * @return the timestamp (ms) of the last driver heartbeat.
120
+ * @return the epoch timestamp (ms) of the last driver heartbeat.
104
121
*/
105
- public long driverHeartbeat ()
122
+ public long driverHeartbeatMs ()
106
123
{
107
- return toDriverBuffer .consumerHeartbeatTime ();
124
+ final int timestampOffset = (toDriverBuffer .capacity () - RingBufferDescriptor .TRAILER_LENGTH ) +
125
+ RingBufferDescriptor .CONSUMER_HEARTBEAT_OFFSET ;
126
+
127
+ return toDriverBuffer .getLongVolatile (timestampOffset );
108
128
}
109
129
110
130
/**
111
131
* Get the number of milliseconds since the last driver heartbeat.
112
132
*
113
133
* @return the number of milliseconds since the last driver heartbeat.
114
134
*/
115
- public long driverHeartbeatAge ()
135
+ public long driverHeartbeatAgeMs ()
116
136
{
117
- return System .currentTimeMillis () - driverHeartbeat ();
137
+ return System .currentTimeMillis () - driverHeartbeatMs ();
118
138
}
119
139
120
- @ Override
121
140
public void close ()
122
141
{
123
- IoUtil .unmap (cncByteBuffer );
142
+ if (!isClosed )
143
+ {
144
+ isClosed = true ;
145
+ IoUtil .unmap (cncByteBuffer );
146
+ }
124
147
}
125
148
}
0 commit comments