From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 25FA26EC55; Fri, 11 Jun 2021 00:28:57 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 25FA26EC55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1623360537; bh=+IHYkXMmLUp0H9vsc7jf94QGGWoHiy3ws54pCovLW7U=; h=Date:To:References:In-Reply-To:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=N9PUluMiD8Bf5AHsPgk+bQy4zC1SC2NY5qGLypRRjj+AhuB6LU9wXPuE4LQQ/v0cx icj0jXUpQ2QJRPRKnXcXyhBA4HLGmaaRmi6/w9ND0gRd8adjvuKq67PwKEjzf3O0F2 /mICYzDMbhWJddF/HkQOE7ZBBwkhZifUjqvPDJIM= Received: from mail-lj1-f175.google.com (mail-lj1-f175.google.com [209.85.208.175]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 4BEB46EC55 for ; Fri, 11 Jun 2021 00:28:55 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 4BEB46EC55 Received: by mail-lj1-f175.google.com with SMTP id r16so6851205ljk.9 for ; Thu, 10 Jun 2021 14:28:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=etg5fn420hutYsUJZ7WF28tKeSQkXYqNiLwpE+k+Lz8=; b=XXR/iiDW/Ih7pKXOmd/JdESATM7g2O0ecUpe1pRlffNrxAvajBvKHqOgbUnNL3spj0 XKx67VfrHIbi35kkaoh4hh3P8AXYYN4xEevW/eOz0Rf5+sAwMFsjxNYaAx47FhpyrEsU EGn0U1Rwup5ahSXMl8pKKntxkr0I1vfwzcaRG8qIrUpQ/mMdP+WAUVayXzwnY/Mzmt1+ I0ernGBim20aQj+qQNbHtvW1RWM+ohd+pKo6LAX15AhzNztdotqTnCL96SrPnXYNzHax veSU4S7qtS+2NDmyQZgX1D82LNKYAwrqCb0d9p50ng0eRkxmwL//ylOJWXaCQoB6uLqR 9cGg== X-Gm-Message-State: AOAM533PYSEO0LFhf9lwIqeJavACiseOovTW5WvaMrMvDmauVkabCMV9 FNCtbIFPydYsDKcFUF8XDV/4fanh+XI= X-Google-Smtp-Source: ABdhPJwPZNoWvnEHsnNap49FtDiF8nfpUelcPXrVuu7nDlwGH9CkHi+b1VCQQbfaDaJ/HTDIr8atvQ== X-Received: by 2002:a2e:b60d:: with SMTP id r13mr456369ljn.314.1623360534043; Thu, 10 Jun 2021 14:28:54 -0700 (PDT) Received: from grain.localdomain ([5.18.171.94]) by smtp.gmail.com with ESMTPSA id m22sm469812ljp.92.2021.06.10.14.28.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Jun 2021 14:28:52 -0700 (PDT) Received: by grain.localdomain (Postfix, from userid 1000) id ABDCA5A0041; Fri, 11 Jun 2021 00:28:51 +0300 (MSK) Date: Fri, 11 Jun 2021 00:28:51 +0300 To: Vladislav Shpilevoy Message-ID: References: <6675abcfa409f1fd6e05a7e7852b42e1a08d1795.1622849790.git.v.shpilevoy@tarantool.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.0.7 (2021-05-04) Subject: Re: [Tarantool-patches] [PATCH 5/6] replication: use 'score' to find a join-master X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Cyrill Gorcunov via Tarantool-patches Reply-To: Cyrill Gorcunov Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" On Thu, Jun 10, 2021 at 10:09:24PM +0200, Vladislav Shpilevoy wrote: > Hi! Thanks for the review! > > On 10.06.2021 17:02, Cyrill Gorcunov wrote: > > On Sat, Jun 05, 2021 at 01:37:59AM +0200, Vladislav Shpilevoy wrote: > >> + int score = 0; > >> /* > >> - * Try to find a replica which has already left > >> - * orphan mode. > >> + * Prefer instances not configured as read-only via box.cfg, and > >> + * not being in read-only state due to any other reason. The > >> + * config is stronger because if it is configured as read-only, > >> + * it is in read-only state for sure, until the config is > >> + * changed. > >> */ > >> - if (ballot->is_ro && !leader_ballot->is_ro) > >> + if (!ballot->is_ro_cfg) > >> + score += 5; > >> + if (!ballot->is_ro) > >> + score += 1; > >> + if (leader_score < score) > >> + goto elect; > >> + if (score < leader_score) > >> continue; > > > > Vlad, if only I'm not missing something obvious we can do simplier > > without branching at all, say > > > > score = (ballot->is_booted << 3) | > > (ballot->is_ro_cfg << 2) | > > (ballot->is_ro << 1); > > > > up to you, just an idea. > > This would work, technically. But TBH I don't see how it is simpler. Due to (x)2^N poly properties when each member is used once and we use additions only. First, we drop the branching which allows to save BPU cycles but since this is not a hot path is doesn't matter. What is matter is the way we choose weigth triplets. Each score addition (in the form they are chosen now) generates strict partial order, such as 1 < 5 < 10, thus if someone occasionally change 5 to 9 this won't longer work properly I think. In case if someone has to add fourth score he _is_ to modify the numbers carefully so the order would remain. In reverse the 2^N poly already has all these properties: each new power won't be equal to a sum of previous powers, so that if fourth score is needed then we simply add (x << 4) to the formula above. Anyway, I'm fine with current code, just wanted to explain why I consider using pows here is more simple and flexible. Since we have only 3 scores by now plain natural triplets should not be a problem.