|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "backend_comm_backoff.h" |
| 21 | +#include <stdlib.h> |
| 22 | +#include <math.h> |
| 23 | +#include "basic_macros.h" |
| 24 | +#include "log.h" |
| 25 | +#include "time_util.h" |
| 26 | + |
| 27 | +#define ELASTIC_APM_CURRENT_LOG_CATEGORY ELASTIC_APM_LOG_CATEGORY_BACKEND_COMM |
| 28 | + |
| 29 | +/** |
| 30 | + * Algorithm is based on Elastic APM agent spec's "Transport errors" section |
| 31 | + * |
| 32 | + * @see https://github.com/elastic/apm/blob/d8cb5607dbfffea819ab5efc9b0743044772fb23/specs/agents/transport.md#transport-errors |
| 33 | + */ |
| 34 | + |
| 35 | +void backendCommBackoff_onSuccess( BackendCommBackoff* thisObj ) |
| 36 | +{ |
| 37 | + ELASTIC_APM_ASSERT_VALID_PTR( thisObj ); |
| 38 | + |
| 39 | + thisObj->errorCount = 0; |
| 40 | + thisObj->waitEndTime = (TimeSpec){ 0 }; |
| 41 | +} |
| 42 | + |
| 43 | +bool backendCommBackoff_getCurrentTime( BackendCommBackoff* thisObj, /* out */ TimeSpec* currentTime ) |
| 44 | +{ |
| 45 | + ResultCode resultCode; |
| 46 | + ELASTIC_APM_CALL_IF_FAILED_GOTO( getClockTimeSpec( /* isRealTime */ false, /* out */ currentTime ) ); |
| 47 | + |
| 48 | + resultCode = resultSuccess; |
| 49 | + finally: |
| 50 | + ELASTIC_APM_LOG_DEBUG_RESULT_CODE_FUNCTION_EXIT(); |
| 51 | + return resultCode == resultSuccess; |
| 52 | + |
| 53 | + failure: |
| 54 | + ELASTIC_APM_LOG_ERROR( "Failed to get current time" ); |
| 55 | + goto finally; |
| 56 | +} |
| 57 | + |
| 58 | +void backendCommBackoff_onError( BackendCommBackoff* thisObj ) |
| 59 | +{ |
| 60 | + ELASTIC_APM_ASSERT_VALID_PTR( thisObj ); |
| 61 | + |
| 62 | + ELASTIC_APM_LOG_DEBUG_FUNCTION_ENTRY(); |
| 63 | + |
| 64 | + /** |
| 65 | + * The grace period should be calculated in seconds using the algorithm min(reconnectCount++, 6) ** 2 ± 10% |
| 66 | + * |
| 67 | + * @see https://github.com/elastic/apm/blob/d8cb5607dbfffea819ab5efc9b0743044772fb23/specs/agents/transport.md#transport-errors |
| 68 | + */ |
| 69 | + enum { maxSequentialErrorsCount = 7 }; |
| 70 | + |
| 71 | + if ( thisObj->errorCount < maxSequentialErrorsCount ) |
| 72 | + { |
| 73 | + ++thisObj->errorCount; |
| 74 | + } |
| 75 | + |
| 76 | + if ( ! backendCommBackoff_getCurrentTime( thisObj, /* out */ &thisObj->waitEndTime ) ) |
| 77 | + { |
| 78 | + // If we cannot get current time we just reset the state to that of no errors |
| 79 | + backendCommBackoff_onSuccess( thisObj ); |
| 80 | + return; |
| 81 | + } |
| 82 | + addDelayToAbsTimeSpec( /* in, out */ &thisObj->waitEndTime, /* delayInNanoseconds */ (long)backendCommBackoff_getTimeToWaitInSeconds( thisObj ) * ELASTIC_APM_NUMBER_OF_NANOSECONDS_IN_SECOND ); |
| 83 | +} |
| 84 | + |
| 85 | +int backendCommBackoff_convertRandomUIntToJitter( UInt randomVal, UInt jitterHalfRange ) |
| 86 | +{ |
| 87 | + double diff = randomVal - ( RAND_MAX / 2.0 ); |
| 88 | + return ( diff >= 0 ? 1 : -1 ) * ( (int) floor( ( jitterHalfRange * fabs( diff ) ) / ( RAND_MAX / 2.0 ) ) ); |
| 89 | +} |
| 90 | + |
| 91 | +UInt backendCommBackoff_getTimeToWaitInSeconds( const BackendCommBackoff* thisObj ) |
| 92 | +{ |
| 93 | + ELASTIC_APM_ASSERT_VALID_PTR( thisObj ); |
| 94 | + |
| 95 | + if ( thisObj->errorCount == 0 ) |
| 96 | + { |
| 97 | + return 0; |
| 98 | + } |
| 99 | + |
| 100 | + UInt reconnectCount = ( thisObj->errorCount - 1); |
| 101 | + double timeToWaitWithoutJitter = pow( reconnectCount, 2 ); |
| 102 | + double jitterHalfRange = timeToWaitWithoutJitter * 0.1; |
| 103 | + UInt jitter = jitterHalfRange < 1 ? 0 : backendCommBackoff_convertRandomUIntToJitter( thisObj->generateRandomUInt( thisObj->generateRandomUIntCtx ), (UInt) floor( jitterHalfRange ) ); |
| 104 | + |
| 105 | + return (int)( round( timeToWaitWithoutJitter ) ) + jitter; |
| 106 | +} |
| 107 | + |
| 108 | +#pragma clang diagnostic push |
| 109 | +#pragma ide diagnostic ignored "UnusedParameter" |
| 110 | +UInt backendCommBackoff_defaultGenerateRandomUInt( void* ctx ) |
| 111 | +#pragma clang diagnostic pop |
| 112 | +{ |
| 113 | + return (UInt) rand(); // NOLINT(cert-msc50-cpp) |
| 114 | +} |
| 115 | + |
| 116 | +bool backendCommBackoff_shouldWait( BackendCommBackoff* thisObj ) |
| 117 | +{ |
| 118 | + if ( thisObj->errorCount == 0 ) |
| 119 | + { |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + TimeSpec currentTime; |
| 124 | + if ( ! backendCommBackoff_getCurrentTime( thisObj, /* out */ ¤tTime ) ) |
| 125 | + { |
| 126 | + // If we cannot get current time we just reset the state to that of no errors |
| 127 | + backendCommBackoff_onSuccess( thisObj ); |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + if ( compareAbsTimeSpecs( &thisObj->waitEndTime, ¤tTime ) <= 0 ) |
| 132 | + { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + char txtOutStreamBuf[ ELASTIC_APM_TEXT_OUTPUT_STREAM_ON_STACK_BUFFER_SIZE ]; |
| 137 | + TextOutputStream txtOutStream = ELASTIC_APM_TEXT_OUTPUT_STREAM_FROM_STATIC_BUFFER( txtOutStreamBuf ); |
| 138 | + ELASTIC_APM_LOG_TRACE( "Left to wait: %s, errorCount: %u", streamTimeSpecDiff( ¤tTime, &thisObj->waitEndTime, &txtOutStream ), thisObj->errorCount ); |
| 139 | + return true; |
| 140 | +} |
0 commit comments