Tarantool development patches archive
 help / color / mirror / Atom feed
From: Vladimir Davydov <vdavydov.dev@gmail.com>
To: kostja@tarantool.org
Cc: tarantool-patches@freelists.org
Subject: [PATCH 02/11] vinyl: refactor quota use/unuse methods
Date: Thu, 20 Sep 2018 12:34:07 +0300	[thread overview]
Message-ID: <213cb4e19dc12ad5b95c0022e2e027a6a88bb98b.1537435404.git.vdavydov.dev@gmail.com> (raw)
In-Reply-To: <cover.1537435404.git.vdavydov.dev@gmail.com>
In-Reply-To: <cover.1537435404.git.vdavydov.dev@gmail.com>

Some minor refactoring targeted at beautifying the code:

 - Rename vy_quota_is_exceeded to vy_quota_may_use and pass the
   requested amount of quota to it explicitly instead of incrementing
   quota before calling this function and decremented after. Move the
   definition closer to vy_quota_use, where this function is used.

 - Implement quota consumption in vy_quota_use via vy_quota_force_use
   so that there's the only function that increments quota.

 - Factor out vy_quota_unuse out of vy_qouta_adjust. This function is
   opposite to vy_quota_force_use.
---
 src/box/vy_quota.c | 55 +++++++++++++++++++++++++-----------------------------
 1 file changed, 25 insertions(+), 30 deletions(-)

diff --git a/src/box/vy_quota.c b/src/box/vy_quota.c
index d3588b6e..8ead5c4b 100644
--- a/src/box/vy_quota.c
+++ b/src/box/vy_quota.c
@@ -68,16 +68,6 @@ static const size_t VY_DEFAULT_DUMP_BANDWIDTH = 10 * 1024 * 1024;
  */
 enum { VY_DUMP_BANDWIDTH_PCT = 10 };
 
-/**
- * Returns true if the quota limit is exceeded and so consumers
- * have to wait.
- */
-static inline bool
-vy_quota_is_exceeded(struct vy_quota *q)
-{
-	return q->used > q->limit;
-}
-
 static void
 vy_quota_timer_cb(ev_loop *loop, ev_timer *timer, int events)
 {
@@ -209,25 +199,29 @@ vy_quota_force_use(struct vy_quota *q, size_t size)
 		q->quota_exceeded_cb(q);
 }
 
+/**
+ * Returns true if the requested amount of quota may be consumed,
+ * or false if consumers have to wait.
+ */
+static bool
+vy_quota_may_use(struct vy_quota *q, size_t size)
+{
+	return q->used + size <= q->limit;
+}
+
 int
 vy_quota_use(struct vy_quota *q, size_t size, double timeout)
 {
-	q->used += size;
-	q->use_curr += size;
-	if (vy_quota_is_exceeded(q)) {
+	if (!vy_quota_may_use(q, size)) {
 		/* Wait for quota. */
 		double start_time = ev_monotonic_now(loop());
 		double deadline = start_time + timeout;
 
 		do {
 			q->quota_exceeded_cb(q);
-			q->used -= size;
-			q->use_curr -= size;
 			if (fiber_cond_wait_deadline(&q->cond, deadline) != 0)
 				return -1; /* timed out */
-			q->used += size;
-			q->use_curr += size;
-		} while (vy_quota_is_exceeded(q));
+		} while (!vy_quota_may_use(q, size));
 
 		double wait_time = ev_monotonic_now(loop()) - start_time;
 		if (wait_time > q->too_long_threshold) {
@@ -240,24 +234,25 @@ vy_quota_use(struct vy_quota *q, size_t size, double timeout)
 		 */
 		fiber_cond_signal(&q->cond);
 	}
-	if (q->used >= q->watermark)
-		q->quota_exceeded_cb(q);
+	vy_quota_force_use(q, size);
 	return 0;
 }
 
+static void
+vy_quota_unuse(struct vy_quota *q, size_t size)
+{
+	assert(q->used >= size);
+	q->used -= size;
+	/* use_curr could have been reset on timeout. */
+	q->use_curr -= MIN(q->use_curr, size);
+	fiber_cond_signal(&q->cond);
+}
+
 void
 vy_quota_adjust(struct vy_quota *q, size_t reserved, size_t used)
 {
-	if (reserved > used) {
-		size_t excess = reserved - used;
-		assert(q->used >= excess);
-		q->used -= excess;
-		if (q->use_curr >= excess)
-			q->use_curr -= excess;
-		else /* was reset by timeout */
-			q->use_curr = 0;
-		fiber_cond_signal(&q->cond);
-	}
+	if (reserved > used)
+		vy_quota_unuse(q, reserved - used);
 	if (reserved < used)
 		vy_quota_force_use(q, used - reserved);
 }
-- 
2.11.0

  parent reply	other threads:[~2018-09-20  9:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-20  9:34 [PATCH 00/11] vinyl: prepare for transaction throttling Vladimir Davydov
2018-09-20  9:34 ` [PATCH 01/11] vinyl: merge vy_quota_release and vy_quota_update_dump_bandwidth Vladimir Davydov
2018-09-20  9:34 ` Vladimir Davydov [this message]
2018-09-20  9:34 ` [PATCH 03/11] vinyl: do not try to trigger dump if it is already in progress Vladimir Davydov
2018-09-20  9:34 ` [PATCH 04/11] vinyl: don't start quota timer until local recovery is complete Vladimir Davydov
2018-09-20  9:34 ` [PATCH 05/11] vinyl: add helper to start scheduler and enable quota on startup Vladimir Davydov
2018-09-25 23:22   ` [tarantool-patches] " Konstantin Osipov
2018-09-20  9:34 ` [PATCH 06/11] vinyl: zap vy_env::memory, read_threads, and write_threads Vladimir Davydov
2018-09-25 23:23   ` [tarantool-patches] " Konstantin Osipov
2018-09-20  9:34 ` [PATCH 07/11] vinyl: do not account zero dump bandwidth Vladimir Davydov
2018-09-25 23:24   ` [tarantool-patches] " Konstantin Osipov
2018-09-20  9:34 ` [PATCH 08/11] vinyl: set quota timer period to 100 ms Vladimir Davydov
2018-09-20  9:34 ` [PATCH 09/11] vinyl: implement basic transaction throttling Vladimir Davydov
2018-09-20  9:34 ` [PATCH 10/11] vinyl: implement quota wait queue without fiber_cond Vladimir Davydov
2018-09-20  9:34 ` [PATCH 11/11] vinyl: split quota consumption rate limit into soft and hard Vladimir Davydov
2018-09-25 23:19 ` [tarantool-patches] Re: [PATCH 00/11] vinyl: prepare for transaction throttling Konstantin Osipov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=213cb4e19dc12ad5b95c0022e2e027a6a88bb98b.1537435404.git.vdavydov.dev@gmail.com \
    --to=vdavydov.dev@gmail.com \
    --cc=kostja@tarantool.org \
    --cc=tarantool-patches@freelists.org \
    --subject='Re: [PATCH 02/11] vinyl: refactor quota use/unuse methods' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox