2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* QueueModel.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FLOW_QUEUEMODEL_H
|
|
|
|
#define FLOW_QUEUEMODEL_H
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "flow/flow.h"
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbrpc/Smoother.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#include "flow/Knobs.h"
|
|
|
|
#include "flow/ActorCollection.h"
|
2021-03-06 03:28:15 +08:00
|
|
|
#include "fdbrpc/TSSComparison.h" // For TSS Metrics
|
|
|
|
#include "fdbrpc/FlowTransport.h" // For Endpoint
|
|
|
|
|
|
|
|
struct TSSEndpointData {
|
|
|
|
UID tssId;
|
|
|
|
Endpoint endpoint;
|
|
|
|
Reference<TSSMetrics> metrics;
|
|
|
|
|
2021-05-29 02:15:52 +08:00
|
|
|
TSSEndpointData(UID tssId, Endpoint endpoint, Reference<TSSMetrics> metrics)
|
|
|
|
: tssId(tssId), endpoint(endpoint), metrics(metrics) {}
|
2021-03-06 03:28:15 +08:00
|
|
|
};
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-02-24 04:20:05 +08:00
|
|
|
// The data structure used for the client-side load balancing algorithm to
|
|
|
|
// decide which storage server to read data from. Conceptually, it tracks the
|
|
|
|
// number of outstanding requests the current client sent to each storage
|
|
|
|
// server. One "QueueData" represents one storage server.
|
2017-05-26 04:48:44 +08:00
|
|
|
struct QueueData {
|
2021-02-24 04:20:05 +08:00
|
|
|
// The current outstanding requests sent by the local client to this storage
|
|
|
|
// server. The number is smoothed out over a continuous timeline.
|
2017-05-26 04:48:44 +08:00
|
|
|
Smoother smoothOutstanding;
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2021-02-24 04:20:05 +08:00
|
|
|
// The last client perceived latency to this storage server.
|
2017-05-26 04:48:44 +08:00
|
|
|
double latency;
|
2021-02-23 07:29:18 +08:00
|
|
|
|
2021-02-24 04:20:05 +08:00
|
|
|
// Represents the "cost" of each storage request. By default, the penalty is
|
2021-02-25 07:15:56 +08:00
|
|
|
// 1 indicating that each outstanding request corresponds 1 outstanding
|
2021-02-24 04:20:05 +08:00
|
|
|
// request. However, storage server can also increase the penalty if it
|
|
|
|
// decides to ask the client to slow down sending requests to it. Penalty
|
|
|
|
// is updated after each LoadBalancedReply.
|
2017-05-26 04:48:44 +08:00
|
|
|
double penalty;
|
2021-02-24 02:21:35 +08:00
|
|
|
|
2021-02-23 07:29:18 +08:00
|
|
|
// Do not consider this storage server if the current time hasn't reach this
|
|
|
|
// time. This field is computed after each request to not repeatedly try the
|
|
|
|
// same storage server that is likely not going to return a valid result.
|
2017-10-20 06:36:32 +08:00
|
|
|
double failedUntil;
|
2021-02-23 07:29:18 +08:00
|
|
|
|
|
|
|
// If the storage server returns a "future version" error, increase above
|
|
|
|
// `failedUntil` by this amount to increase the backoff time.
|
2017-10-20 06:36:32 +08:00
|
|
|
double futureVersionBackoff;
|
2021-02-23 07:29:18 +08:00
|
|
|
|
|
|
|
// If the current time has reached this time, and this storage server still
|
|
|
|
// hasn't returned a valid result, increase above `futureVersionBackoff`
|
|
|
|
// to increase the future backoff amount.
|
2017-10-20 06:36:32 +08:00
|
|
|
double increaseBackoffTime;
|
2021-03-06 03:28:15 +08:00
|
|
|
|
|
|
|
// a bit of a hack to store this here, but it's the only centralized place for per-endpoint tracking
|
|
|
|
Optional<TSSEndpointData> tssData;
|
|
|
|
|
2021-02-24 02:21:35 +08:00
|
|
|
QueueData()
|
2021-07-23 13:48:27 +08:00
|
|
|
: smoothOutstanding(FLOW_KNOBS->QUEUE_MODEL_SMOOTHING_AMOUNT), latency(0.001), penalty(1.0), failedUntil(0),
|
2021-02-24 02:21:35 +08:00
|
|
|
futureVersionBackoff(FLOW_KNOBS->FUTURE_VERSION_INITIAL_BACKOFF), increaseBackoffTime(0) {}
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef double TimeEstimate;
|
|
|
|
|
|
|
|
class QueueModel {
|
|
|
|
public:
|
2021-02-23 07:29:18 +08:00
|
|
|
// Finishes the request sent to storage server with `id`.
|
|
|
|
// - latency: the measured client-side latency of the request.
|
|
|
|
// - penalty: the server side penalty sent along with the response from
|
2021-02-24 04:20:05 +08:00
|
|
|
// the storage server. Requires >= 1.
|
2021-02-23 07:29:18 +08:00
|
|
|
// - delta: Update server `id`'s queue model by substract this amount.
|
|
|
|
// This value should be the value returned by `addRequest` below.
|
2021-02-24 02:21:35 +08:00
|
|
|
// - clean: indicates whether the there was an error or not.
|
2021-02-23 07:29:18 +08:00
|
|
|
// - futureVersion: indicates whether there was "future version" error or
|
|
|
|
// not.
|
2021-02-24 02:21:35 +08:00
|
|
|
void endRequest(uint64_t id, double latency, double penalty, double delta, bool clean, bool futureVersion);
|
|
|
|
QueueData& getMeasurement(uint64_t id);
|
|
|
|
|
|
|
|
// Starts a new request to storage server with `id`. If the storage
|
|
|
|
// server contains a penalty, add it to the queue size, and return the
|
|
|
|
// penalty. The returned penalty should be passed as `delta` to `endRequest`
|
|
|
|
// to make `smoothOutstanding` to reflect the real storage queue size.
|
|
|
|
double addRequest(uint64_t id);
|
2017-05-26 04:48:44 +08:00
|
|
|
double secondMultiplier;
|
|
|
|
double secondBudget;
|
2021-02-24 02:21:35 +08:00
|
|
|
PromiseStream<Future<Void>> addActor;
|
2017-05-26 04:48:44 +08:00
|
|
|
Future<Void> laggingRequests; // requests for which a different recipient already answered
|
2021-05-13 02:53:20 +08:00
|
|
|
PromiseStream<Future<Void>> addTSSActor;
|
|
|
|
Future<Void> tssComparisons; // requests for which a different recipient already answered
|
2017-05-26 04:48:44 +08:00
|
|
|
int laggingRequestCount;
|
2021-05-13 02:53:20 +08:00
|
|
|
int laggingTSSCompareCount;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-08-28 08:07:47 +08:00
|
|
|
// Updates this endpoint data to duplicate requests to the specified TSS endpoint
|
2021-05-29 02:19:42 +08:00
|
|
|
void updateTssEndpoint(uint64_t endpointId, const TSSEndpointData& endpointData);
|
2021-06-03 23:31:16 +08:00
|
|
|
|
2021-08-28 08:07:47 +08:00
|
|
|
// Removes the TSS mapping from this endpoint to stop duplicating requests to a TSS endpoint
|
2021-05-29 02:15:52 +08:00
|
|
|
void removeTssEndpoint(uint64_t endpointId);
|
2021-06-03 23:31:16 +08:00
|
|
|
|
2021-08-28 08:07:47 +08:00
|
|
|
// Retrieves the data for this endpoint's pair TSS endpoint, if present
|
2021-03-06 03:28:15 +08:00
|
|
|
Optional<TSSEndpointData> getTssData(uint64_t endpointId);
|
|
|
|
|
2021-05-29 02:15:52 +08:00
|
|
|
QueueModel() : secondMultiplier(1.0), secondBudget(0), laggingRequestCount(0) {
|
2021-02-24 02:21:35 +08:00
|
|
|
laggingRequests = actorCollection(addActor.getFuture(), &laggingRequestCount);
|
2021-05-13 02:53:20 +08:00
|
|
|
tssComparisons = actorCollection(addTSSActor.getFuture(), &laggingTSSCompareCount);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2021-05-13 02:53:20 +08:00
|
|
|
~QueueModel() {
|
|
|
|
laggingRequests.cancel();
|
|
|
|
tssComparisons.cancel();
|
|
|
|
}
|
2021-02-24 02:21:35 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
2019-03-28 11:56:44 +08:00
|
|
|
std::unordered_map<uint64_t, QueueData> data;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* old queue model
|
|
|
|
class QueueModel {
|
|
|
|
public:
|
2021-02-24 02:21:35 +08:00
|
|
|
QueueModel() : new_index(0) {
|
|
|
|
total_time[0] = 0;
|
|
|
|
total_time[1] = 0;
|
|
|
|
}
|
|
|
|
void addMeasurement( uint64_t id, QueueDetails qd );
|
|
|
|
TimeEstimate getTimeEstimate( uint64_t id );
|
|
|
|
TimeEstimate getAverageTimeEstimate();
|
|
|
|
QueueDetails getMeasurement( uint64_t id );
|
|
|
|
void expire();
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
private:
|
2021-02-24 02:21:35 +08:00
|
|
|
std::map<uint64_t, QueueDetails> data[2];
|
|
|
|
double total_time[2];
|
|
|
|
int new_index; // data[new_index] is the new data
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2021-06-03 23:31:16 +08:00
|
|
|
#endif
|