Skip to content

Commit e9779d7

Browse files
all: use a proper log id which can reference channels, subchannels, and transports
1 parent 7ec8167 commit e9779d7

File tree

13 files changed

+114
-38
lines changed

13 files changed

+114
-38
lines changed

core/src/main/java/io/grpc/inprocess/InProcessTransport.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
import io.grpc.internal.ClientStream;
4545
import io.grpc.internal.ClientStreamListener;
4646
import io.grpc.internal.ConnectionClientTransport;
47-
import io.grpc.internal.GrpcUtil;
47+
import io.grpc.internal.LogId;
4848
import io.grpc.internal.ManagedClientTransport;
4949
import io.grpc.internal.NoopClientStream;
5050
import io.grpc.internal.ServerStream;
@@ -71,6 +71,7 @@
7171
class InProcessTransport implements ServerTransport, ConnectionClientTransport {
7272
private static final Logger log = Logger.getLogger(InProcessTransport.class.getName());
7373

74+
private final LogId logId = LogId.allocate(getClass().getName());
7475
private final String name;
7576
private ServerTransportListener serverTransportListener;
7677
private Attributes serverStreamAttributes;
@@ -203,8 +204,8 @@ public String toString() {
203204
}
204205

205206
@Override
206-
public String getLogId() {
207-
return GrpcUtil.getLogId(this);
207+
public LogId getLogId() {
208+
return logId;
208209
}
209210

210211
@Override

core/src/main/java/io/grpc/internal/DelayedClientTransport.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@
6363
* streams are transferred to the given transport, thus this transport won't own any stream.
6464
*/
6565
class DelayedClientTransport implements ManagedClientTransport {
66+
67+
private final LogId lodId = LogId.allocate(getClass().getName());
68+
6669
private final Object lock = new Object();
6770

6871
private final Executor streamCreationExecutor;
@@ -455,9 +458,10 @@ public void run() {
455458
}
456459
}
457460

461+
// TODO(carl-mastrangelo): remove this once the Subchannel change is in.
458462
@Override
459-
public final String getLogId() {
460-
return GrpcUtil.getLogId(this);
463+
public LogId getLogId() {
464+
return lodId;
461465
}
462466

463467
@VisibleForTesting

core/src/main/java/io/grpc/internal/ForwardingConnectionClientTransport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void ping(PingCallback callback, Executor executor) {
7373
}
7474

7575
@Override
76-
public String getLogId() {
76+
public LogId getLogId() {
7777
return delegate().getLogId();
7878
}
7979

core/src/main/java/io/grpc/internal/GrpcUtil.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -535,13 +535,6 @@ public Long parseAsciiString(String serialized) {
535535
}
536536
}
537537

538-
/**
539-
* The canonical implementation of {@link WithLogId#getLogId}.
540-
*/
541-
public static String getLogId(WithLogId subject) {
542-
return subject.getClass().getSimpleName() + "@" + Integer.toHexString(subject.hashCode());
543-
}
544-
545538
private GrpcUtil() {}
546539

547540
private static String getImplementationVersion() {

core/src/main/java/io/grpc/internal/InternalSubchannel.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
final class InternalSubchannel implements WithLogId {
7373
private static final Logger log = Logger.getLogger(InternalSubchannel.class.getName());
7474

75+
7576
private final Object lock = new Object();
77+
private final LogId logId = LogId.allocate(getClass().getName());
7678
private final EquivalentAddressGroup addressGroup;
7779
private final String authority;
7880
private final String userAgent;
@@ -119,12 +121,12 @@ final class InternalSubchannel implements WithLogId {
119121
* also be present.
120122
*/
121123
@GuardedBy("lock")
122-
private final Collection<ManagedClientTransport> transports =
123-
new ArrayList<ManagedClientTransport>();
124+
private final Collection<ConnectionClientTransport> transports =
125+
new ArrayList<ConnectionClientTransport>();
124126

125127
// Must only be used from channelExecutor
126-
private final InUseStateAggregator2<ManagedClientTransport> inUseStateAggregator =
127-
new InUseStateAggregator2<ManagedClientTransport>() {
128+
private final InUseStateAggregator2<ConnectionClientTransport> inUseStateAggregator =
129+
new InUseStateAggregator2<ConnectionClientTransport>() {
128130
@Override
129131
void handleInUse() {
130132
callback.onInUse(InternalSubchannel.this);
@@ -329,7 +331,7 @@ public void run() {
329331
}
330332

331333
private void handleTransportInUseState(
332-
final ManagedClientTransport transport, final boolean inUse) {
334+
final ConnectionClientTransport transport, final boolean inUse) {
333335
channelExecutor.execute(new Runnable() {
334336
@Override
335337
public void run() {
@@ -358,8 +360,8 @@ private void cancelReconnectTask() {
358360
}
359361

360362
@Override
361-
public String getLogId() {
362-
return GrpcUtil.getLogId(this);
363+
public LogId getLogId() {
364+
return logId;
363365
}
364366

365367
@VisibleForTesting
@@ -371,10 +373,10 @@ ConnectivityState getState() {
371373

372374
/** Listener for real transports. */
373375
private class TransportListener implements ManagedClientTransport.Listener {
374-
final ManagedClientTransport transport;
376+
final ConnectionClientTransport transport;
375377
final SocketAddress address;
376378

377-
TransportListener(ManagedClientTransport transport, SocketAddress address) {
379+
TransportListener(ConnectionClientTransport transport, SocketAddress address) {
378380
this.transport = transport;
379381
this.address = address;
380382
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2016, Google Inc. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* * Neither the name of Google Inc. nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package io.grpc.internal;
33+
34+
import java.util.concurrent.atomic.AtomicLong;
35+
36+
/**
37+
* A loggable ID, unique for the duration of the program.
38+
*/
39+
public final class LogId {
40+
private static final AtomicLong idAlloc = new AtomicLong();
41+
42+
/**
43+
* @param tag a loggable tag associated with this ID.
44+
*/
45+
public static LogId allocate(String tag) {
46+
return new LogId(tag, idAlloc.incrementAndGet());
47+
}
48+
49+
private final String tag;
50+
private final long id;
51+
52+
private LogId(String tag, long id) {
53+
this.tag = tag;
54+
this.id = id;
55+
}
56+
57+
public long getId() {
58+
return id;
59+
}
60+
61+
public String getTag() {
62+
return tag;
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return tag + "-" + id;
68+
}
69+
}

core/src/main/java/io/grpc/internal/ManagedChannelImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public final class ManagedChannelImpl extends ManagedChannel implements WithLogI
130130
private final Executor executor;
131131
private final boolean usingSharedExecutor;
132132
private final Object lock = new Object();
133+
private final LogId logId = LogId.allocate(getClass().getName());
133134

134135
private final DecompressorRegistry decompressorRegistry;
135136
private final CompressorRegistry compressorRegistry;
@@ -745,8 +746,8 @@ public OobTransportProvider<ClientTransport> createOobTransportProvider(
745746
};
746747

747748
@Override
748-
public String getLogId() {
749-
return GrpcUtil.getLogId(this);
749+
public LogId getLogId() {
750+
return logId;
750751
}
751752

752753
private static class NameResolverListenerImpl implements NameResolver.Listener {

core/src/main/java/io/grpc/internal/TransportSet.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ final class TransportSet extends ManagedChannel implements WithLogId {
7373

7474
private final CountDownLatch terminatedLatch = new CountDownLatch(1);
7575
private final Object lock = new Object();
76+
private final LogId logId = LogId.allocate(getClass().getName());
7677
private final EquivalentAddressGroup addressGroup;
7778
private final String authority;
7879
private final String userAgent;
@@ -352,8 +353,8 @@ private void cancelReconnectTask() {
352353
}
353354

354355
@Override
355-
public String getLogId() {
356-
return GrpcUtil.getLogId(this);
356+
public LogId getLogId() {
357+
return logId;
357358
}
358359

359360
@Override

core/src/main/java/io/grpc/internal/WithLogId.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,5 @@ public interface WithLogId {
4343
* <p>The subclasses of this interface usually want to include the log ID in their {@link
4444
* #toString} results.
4545
*/
46-
String getLogId();
46+
LogId getLogId();
4747
}

core/src/test/java/io/grpc/internal/InternalSubchannelTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public void connectIsLazy() {
422422
// No scheduled tasks that would ever try to reconnect ...
423423
assertEquals(0, fakeClock.numPendingTasks());
424424
assertEquals(0, fakeExecutor.numPendingTasks());
425-
425+
426426
// ... until it's requested.
427427
internalSubchannel.obtainActiveTransport();
428428
assertExactCallbackInvokes("onStateChange:CONNECTING");
@@ -434,7 +434,7 @@ public void connectIsLazy() {
434434
public void shutdownWhenReady() throws Exception {
435435
SocketAddress addr = mock(SocketAddress.class);
436436
createInternalSubchannel(addr);
437-
437+
438438
internalSubchannel.obtainActiveTransport();
439439
MockClientTransportInfo transportInfo = transports.poll();
440440
transportInfo.listener.transportReady();
@@ -528,7 +528,7 @@ public void shutdownBeforeTransportReady() throws Exception {
528528
public void shutdownNow() throws Exception {
529529
SocketAddress addr = mock(SocketAddress.class);
530530
createInternalSubchannel(addr);
531-
531+
532532
internalSubchannel.obtainActiveTransport();
533533
MockClientTransportInfo t1 = transports.poll();
534534
t1.listener.transportReady();
@@ -565,8 +565,8 @@ public void obtainTransportAfterShutdown() throws Exception {
565565
@Test
566566
public void logId() {
567567
createInternalSubchannel(mock(SocketAddress.class));
568-
assertEquals("InternalSubchannel@" + Integer.toHexString(internalSubchannel.hashCode()),
569-
internalSubchannel.getLogId());
568+
569+
assertNotNull(internalSubchannel.getLogId());
570570
}
571571

572572
@Test

0 commit comments

Comments
 (0)