Improve segmented journal actor metrics
[controller.git] / opendaylight / md-sal / cds-access-api / src / main / java / org / opendaylight / controller / cluster / access / concepts / ResponseEnvelope.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.cluster.access.concepts;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import java.io.IOException;
13 import java.io.ObjectInput;
14 import java.io.ObjectOutput;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.concepts.WritableObjects;
17
18 public abstract class ResponseEnvelope<T extends Response<?, ?>> extends Envelope<T> {
19     interface SerialForm<T extends Response<?, ?>, E extends ResponseEnvelope<T>> extends Envelope.SerialForm<T, E> {
20         @Override
21         default void writeExternal(final ObjectOutput out, final @NonNull E envelope) throws IOException {
22             Envelope.SerialForm.super.writeExternal(out, envelope);
23             WritableObjects.writeLong(out, envelope.getExecutionTimeNanos());
24         }
25
26         @Override
27         default E readExternal(final ObjectInput in, final long sessionId, final long txSequence, final T message)
28                 throws IOException {
29             return readExternal(in, sessionId, txSequence, message, WritableObjects.readLong(in));
30         }
31
32         E readExternal(ObjectInput in, long sessionId, long txSequence, T message, long executionTimeNanos);
33     }
34
35     @java.io.Serial
36     private static final long serialVersionUID = 1L;
37
38     private final long executionTimeNanos;
39
40     ResponseEnvelope(final T message, final long sessionId, final long txSequence, final long executionTimeNanos) {
41         super(message, sessionId, txSequence);
42         checkArgument(executionTimeNanos >= 0, "Negative executionTime");
43         this.executionTimeNanos = executionTimeNanos;
44     }
45
46     /**
47      * Return the time the request took to execute in nanoseconds. This may not reflect the actual CPU time, but rather
48      * a measure of the complexity involved in servicing the original request.
49      *
50      * @return Time the request took to execute in nanoseconds
51      */
52     public final long getExecutionTimeNanos() {
53         return executionTimeNanos;
54     }
55 }