Skip to content

Commit

Permalink
[pinpoint-apm#182] agentInfo retry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
feelform committed Jun 28, 2024
1 parent 6b6e0d3 commit 407a5bd
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 251 deletions.
62 changes: 62 additions & 0 deletions lib/client/call-arguments-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Pinpoint Node.js Agent
* Copyright 2020-present NAVER Corp.
* Apache License v2.0
*/

'use strict'

const grpc = require('@grpc/grpc-js')

class CallArguments {
constructor(callback) {
this.callback = callback
}

getCallback() {
return this.callback
}

getMetadata() {
return this.metadata
}

getOptions() {
const options = this.options
if (this.deadlineMilliseconds) {
const deadline = new Date()
deadline.setMilliseconds(deadline.getMilliseconds() + this.deadlineMilliseconds)
options.deadline = deadline
}
return options
}
}

const DEFAULT_CLIENT_REQUEST_TIMEOUT = 6000
class CallArgumentsBuilder {
constructor(callback) {
this.callback = callback
this.metadata = new grpc.Metadata()
this.options = {}
}

setDeadlineMilliseconds(deadlineMilliseconds) {
this.deadlineMilliseconds = deadlineMilliseconds
return this
}

setMetadata(key, value) {
this.metadata.set(key, value)
return this
}

build() {
const callArguments = new CallArguments(this.callback)
callArguments.metadata = this.metadata
callArguments.options = this.options
callArguments.deadlineMilliseconds = this.deadlineMilliseconds || DEFAULT_CLIENT_REQUEST_TIMEOUT
return callArguments
}
}

module.exports = CallArgumentsBuilder
28 changes: 19 additions & 9 deletions lib/client/grpc-data-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ const GrpcUnaryRPC = require('./grpc-unary-rpc')
const Scheduler = require('../utils/scheduler')
const makeAgentInformationMetadataInterceptor = require('./grpc/make-agent-information-metadata-interceptor')
const socketIdInterceptor = require('./grpc/socketid-interceptor')
const retryUnaryRequestInterceptor = require('./grpc/retry-unary-request-interceptor')
const Deadline = require('./grpc/deadline')
const CallArgumentsBuilder = require('./call-arguments-builder')

// AgentInfoSender.java
// refresh daily
Expand Down Expand Up @@ -46,7 +45,6 @@ class GrpcDataSender {
this.initializeStatStream(collectorIp, collectorStatPort, config)
this.initializePingStream()
this.initializeAgentInfoScheduler()
this.deadline = new Deadline()
}

close() {
Expand Down Expand Up @@ -76,10 +74,10 @@ class GrpcDataSender {

initializeClients(collectorIp, collectorTcpPort) {
this.agentClient = new services.AgentClient(collectorIp + ":" + collectorTcpPort, grpc.credentials.createInsecure(),
{ interceptors: [retryUnaryRequestInterceptor, makeAgentInformationMetadataInterceptor(this.agentInfo), socketIdInterceptor] })
{ interceptors: [makeAgentInformationMetadataInterceptor(this.agentInfo), socketIdInterceptor] })

this.metadataClient = new services.MetadataClient(collectorIp + ":" + collectorTcpPort, grpc.credentials.createInsecure(),
{ interceptors: [retryUnaryRequestInterceptor, makeAgentInformationMetadataInterceptor(this.agentInfo)] })
{ interceptors: [makeAgentInformationMetadataInterceptor(this.agentInfo)] })
this.requestApiMetaData = new GrpcUnaryRPC('requestApiMetaData', this.metadataClient, this.metadataClient.requestApiMetaData, DEFAULT_METADATA_RETRY_DELAY_MILLIS, DEFAULT_METADATA_RETRY_MAX_COUNT)
this.requestStringMetaData = new GrpcUnaryRPC('requestStringMetaData', this.metadataClient, this.metadataClient.requestStringMetaData, DEFAULT_METADATA_RETRY_DELAY_MILLIS, DEFAULT_METADATA_RETRY_MAX_COUNT)
this.requestSqlMetaData = new GrpcUnaryRPC('requestSqlMetaData', this.metadataClient, this.metadataClient.requestSqlMetaData, DEFAULT_METADATA_RETRY_DELAY_MILLIS, DEFAULT_METADATA_RETRY_MAX_COUNT)
Expand Down Expand Up @@ -120,13 +118,24 @@ class GrpcDataSender {
return DEFAULT_AGENT_INFO_REFRESH_INTERVAL_MS
}

sendAgentInfo(agentInfo, callback) {
sendAgentInfo(agentInfo, callArguments) {
const pAgentInfo = dataConvertor.convertAgentInfo(agentInfo)
if (log.isDebug()) {
log.debug(`sendAgentInfo pAgentInfo: ${pAgentInfo}`)
}
const deadline = this.deadline.getDeadline()
this.agentClient.requestAgentInfo(pAgentInfo, { deadline }, (err, response) => {

if (typeof callArguments === 'function') {
callArguments = new CallArgumentsBuilder(callArguments).build()
}

if (!callArguments) {
callArguments = new CallArgumentsBuilder().build()
}

const metadata = callArguments.getMetadata()
let options = callArguments.getOptions()
const callback = callArguments.getCallback()
this.agentClient.requestAgentInfo(pAgentInfo, metadata, options, (err, response) => {
if (typeof callback === 'function') {
callback(err, response)
}
Expand All @@ -135,7 +144,8 @@ class GrpcDataSender {
this.closeScheduler()
if (this.agentInfoDailyScheduler) {
this.removeJobForAgentInfo = this.agentInfoDailyScheduler.addJob(() => {
this.agentClient.requestAgentInfo(pAgentInfo, { deadline }, (err, response) => {
options = callArguments.getOptions()
this.agentClient.requestAgentInfo(pAgentInfo, metadata, options, (err, response) => {
if (typeof callback === 'function') {
callback(err, response)
}
Expand Down
19 changes: 0 additions & 19 deletions lib/client/grpc/deadline.js

This file was deleted.

73 changes: 0 additions & 73 deletions lib/client/grpc/retry-unary-request-interceptor.js

This file was deleted.

Loading

0 comments on commit 407a5bd

Please sign in to comment.