text:
local chat = {
	lines = {},
	active = false,
	enterText = "",
	time = 0,
}

local CHAT_WIDTH = 300
local chatSide = "left"
local CHAT_INPUT_CHARACTERS = 100
-- Used for line breaking:
local letter = "[%z\1-\127\194-\244][\128-\191]*"
local word = "[%S]* "	-- not space followed by space

local colNormal = {255,255,255,255}
local colServer = {255,200,64,255}

local panel = require( "panel" )

function chat:init()
	CHAT_WIDTH = math.min(love.graphics.getWidth()/2 - 40, 500)
end

function chat:reset()
	self.lines = {} -- "", "", "", "", "", "", "", "Press enter to chat.", "Welcome!" }
	for k = 1, 11 do
		table.insert( self.lines, {txt = "", col=colNormal} )
	end
	table.insert( self.lines, {txt = "Press enter to chat.", col=colServer} )
	self.panel = panel:new( 0, 0, CHAT_WIDTH, 20*(#self.lines) + 10 )
end

function chat:show()
end

function chat:draw()
	local fontHeight = love.graphics.getFont():getHeight()
	local x
	local y = love.graphics.getHeight() - 20*(#self.lines+1) - 40

	if map.loaded and client and client:getID() then
		if map:hasCar( client:getID() ) then
			local c = map:getCar( client:getID() )
			if c.vY > 0 then
			if chatSide == "left" then
				if c.vX < 0 then
					chatSide = "right"
				end
			else
				if c.vX > 0 then
					chatSide = "left"
				end
			end
		end
		end
	end

	if chatSide == "left" then
		x = 20
	else
		x = love.graphics.getWidth() - 20 - CHAT_WIDTH
	end

	--love.graphics.rectangle( "fill", x, y,
			--CHAT_WIDTH, 20*(#self.lines) + 10 )
	self.panel:draw( x, y )

	x = x + 5
	y = y + 10
	for k = 1, #self.lines do
		love.graphics.setColor( self.lines[k].col )
		love.graphics.print( self.lines[k].txt, x, y )
		y = y + 20
	end

	local x, y = 20, love.graphics.getHeight() - 40
	if self.active then

		local str = self.enterText

		local inputLen = love.graphics.getFont():getWidth( "Say: " .. str )
		inputLen = math.max(inputLen, 50 )

		love.graphics.setColor( 0, 0, 0, 200 )
		love.graphics.rectangle( "fill",
			x, y, inputLen + 13, fontHeight + 10 )

		love.graphics.setColor( 255, 255, 255, 255 )
		if math.sin(self.time*3) > 0 then
			love.graphics.print( "Say: " .. str .. "|", x + 5, y + 5 )
		else
			love.graphics.print( "Say: " .. str, x + 5, y + 5 )
		end
	end

end

function chat:update( dt )
	if STATE == "Lobby" or STATE == "Game" then
		if self.active then
			self.time = self.time + dt
		end
	end
end

function chat:newLine( text, col )

	-- remove leading whitespace:
	text = text:match( "%s*(.*)" )

	local font = love.graphics.getFont()

	local restStr
	-- Wrap text if necessary:
	local wLines = {}	-- lines that have been wrapped

	if font:getWidth( text ) <= CHAT_WIDTH - 10 then
		table.insert( wLines, text )
	else
		local restLine = text .. " "
		local tmpLine, shortLine
		while #restLine > 0 do
			local i = 1
			local breakingCondition = false
			tmpLine = nil
			shortLine = nil
			repeat		-- look for spaces!
				tmpLine = restLine:match( word:rep(i) )
				if tmpLine then
					if font:getWidth(tmpLine) > CHAT_WIDTH - 10 then
						breakingCondition = true
					else
						shortLine = tmpLine
					end
				else
					breakingCondition = true
				end
				i = i + 1
			until breakingCondition
			if not shortLine then -- if there weren't enough spaces then:
				breakingCondition = false
				i = 1
				repeat			-- ... look for letters:
					tmpLine = restLine:match( letter:rep(i) )
					if tmpLine then
						if font:getWidth(tmpLine) > self.width then
							breakingCondition = true
						else
							shortLine = tmpLine
						end
					else
						breakingCondition = true
					end
					i = i + 1
				until breakingCondition
			end
			table.insert( wLines, shortLine )
			restLine = restLine:sub( #shortLine+1 )
		end
	end

	for k, l in ipairs(wLines) do

		-- Add the text to the table of text lines:
		for k = 1, #self.lines-1 do
			self.lines[k] = self.lines[k+1]
		end
		self.lines[#self.lines] = {txt = l, col = col}

	end
end

function chat:newLineSpeech( text )
	self:newLine( text, colNormal )
end
function chat:newLineServer( text )
	self:newLine( text, colServer )
end

function chat:keypressed( key )
	if STATE == "Game" or STATE == "Lobby" then
		if key == "return" then
			if self.active then
				if client then
					if #self.enterText > 0 then
						client:send( CMD.CHAT, self.enterText )
					end
				end
				self.enterText = ""
				self.active = false
			else
				if client then
					self.active = true
				end
			end
		elseif key == "backspace" then
			if #self.enterText > 0 then
				self.enterText = self.enterText:sub( 1, #self.enterText - 1 )
				-- Check if the last char is part of an umlaut or similar. If so, remove it:
				local last = self.enterText:sub(-1)
				local byte = string.byte(last)
				if byte and byte >= 194 then
					self.enterText = self.enterText:sub( 1, #self.enterText - 1 )
				end
			end
		elseif key == "escape" then
			self.enterText = ""
			self.active = false
		end
	end
end

function chat:textinput( letter )
	if STATE == "Game" or STATE == "Lobby" then
		if self.active then
			if letter ~= "|" then
				if love.graphics.getFont():getWidth( "Say: " .. self.enterText )
						< love.graphics.getWidth() - 60 then
					self.enterText = self.enterText .. letter
				end
			end
		end
	end
end

return chat


text: import './ServiceDispatcher.js';

redpajama_set_name: RedPajamaGithub


text:
<?php
use PhlyBlog\AuthorEntity;
use PhlyBlog\EntryEntity;

$entry  = new EntryEntity();
$author = new AuthorEntity();
$author->fromArray(array (
  'id' => 'crazyhorse',
  'name' => 'Crazy Horse',
  'email' => 'crazyhorse@siouxnation.org',
  'url' => 'http://crazyhorse.siouxnation.org',
));

$entry->setId('4f8706cf6265b-post-name');
$entry->setTitle('4f8706cf6265b Post');
$entry->setAuthor($author);
$entry->setDraft(false);
$entry->setPublic(true);
$entry->setCreated(new DateTime('2008:12:07 19:48:48'));
$entry->setUpdated(new DateTime('2008:12:07 19:48:48'));
$entry->setTimezone('America/Chicago');
$entry->setTags(array (
  0 => 'holiday',
  1 => 'personal',
  2 => 'programming',
  3 => 'thoughts',
  4 => 'war',
  5 => 'literature',
  6 => 'children',
  7 => 'draft',
  8 => 'conferences',
  9 => 'php',
));

$body =<<<'EOT'
This is it!
EOT;
$entry->setBody($body);

$extended =<<<'EOT'
This is the extended portion of the entry.
EOT;
$entry->setExtended($extended);

return $entry;


text:
namespace DW.ELA.LogModel
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using DW.ELA.Interfaces;
    using DW.ELA.Interfaces.Events;
    using Newtonsoft.Json.Linq;

    public static class JournalEventConverter
    {
        private static readonly IReadOnlyDictionary<string, Type> eventTypes;

        static JournalEventConverter()
        {
            var baseType = typeof(JournalEvent);
            var exampleType = typeof(LoadGame);

            eventTypes = baseType
               .Assembly
               .GetTypes()
               .Where(t => t.IsSubclassOf(baseType))
               .ToDictionary(t => t.Name.ToLowerInvariant(), t => t);
            Debug.Assert(eventTypes.Count > 0, "Must have events");
            Debug.Assert(eventTypes.Values.Contains(exampleType), "Event LoadGame not loaded");
        }

        public static JournalEvent Convert(JObject jObject)
        {
            string eventName = jObject["event"]?.ToString()?.ToLowerInvariant();
            JournalEvent result;
            if (string.IsNullOrWhiteSpace(eventName))
                throw new ArgumentException("Empty event name", nameof(jObject));

            if (eventTypes.ContainsKey(eventName))
                result = (JournalEvent)jObject.ToObject(eventTypes[eventName]);
            else
                result = jObject.ToObject<JournalEvent>();

            result.Raw = jObject;
            return result;
        }
    }
}


text:
from typing import Dict
from pandas import DataFrame
from lib.data_source import DataSource
from lib.time import datetime_isoformat
from lib.utils import table_rename

class LuxembourgDataSource(DataSource):
    def parse_dataframes(
        self, dataframes: Dict[str, DataFrame], aux: Dict[str, DataFrame], **parse_opts
    ) -> DataFrame:
        # The headers are a bit funny-looking, so we must manually manipulate them first
        data = dataframes[0]
        data.columns = [col.split("|")[0].split("~")[0] for col in data.iloc[0]]
        data = data.iloc[1:]

        data = table_rename(
            data,
            {
                "Date": "date",
                "Nombre de personnes en soins intensifs": "current_intensive_care",
                "Nombre cumulé de décès": "total_deceased",
                "Nombre de personnes testées COVID+": "new_tested",
            },
            drop=True,
        )

        # Get date in ISO format
        data["date"] = data["date"].apply(lambda x: datetime_isoformat(x, "%d/%m/%Y"))

        # Only country-level data is provided
        data["key"] = "LU"

        # Output the results
        return data


text:
<?php

namespace FOS\Message\Driver;

use FOS\Message\Model\ConversationInterface;
use FOS\Message\Model\MessageInterface;
use FOS\Message\Model\PersonInterface;

/**
 * Abstract driver providing model class customization.
 *
 * @author Titouan Galopin <galopintitouan@gmail.com>
 */
abstract class AbstractDriver implements DriverInterface
{
    /**
     * @var string
     */
    private $conversationClass;

    /**
     * @var string
     */
    private $conversationPersonClass;

    /**
     * @var string
     */
    private $messageClass;

    /**
     * @var string
     */
    private $messagePersonClass;

    /**
     * Constructor.
     *
     * @param string $conversationClass
     * @param string $conversationPersonClass
     * @param string $messageClass
     * @param string $messagePersonClass
     */
    public function __construct($conversationClass, $conversationPersonClass, $messageClass, $messagePersonClass)
    {
        $this->conversationClass = $conversationClass;
        $this->conversationPersonClass = $conversationPersonClass;
        $this->messageClass = $messageClass;
        $this->messagePersonClass = $messagePersonClass;
    }

    /**
     * @return string
     */
    public function getConversationClass()
    {
        return $this->conversationClass;
    }

    /**
     * @return string
     */
    public function getConversationPersonClass()
    {
        return $this->conversationPersonClass;
    }

    /**
     * @return string
     */
    public function getMessageClass()
    {
        return $this->messageClass;
    }

    /**
     * @return string
     */
    public function getMessagePersonClass()
    {
        return $this->messagePersonClass;
    }

    /**
     * {@inheritdoc}
     */
    public function createConversationModel()
    {
        $class = $this->conversationClass;

        return new $class();
    }

    /**
     * {@inheritdoc}
     */
    public function createConversationPersonModel(ConversationInterface $conversation, PersonInterface $person)
    {
        $class = $this->conversationPersonClass;

        return new $class($conversation, $person);
    }

    /**
     * {@inheritdoc}
     */
    public function createMessageModel(ConversationInterface $conversation, PersonInterface $sender, $body)
    {
        $class = $this->messageClass;

        return new $class($conversation, $sender, $body);
    }

    /**
     * {@inheritdoc}
     */
    public function createMessagePersonModel(MessageInterface $message, PersonInterface $person)
    {
        $class = $this->messagePersonClass;

        return new $class($message, $person);
    }
}


text:
package sim

import (
	"github.com/itglobal/dashboard/tile"

	"github.com/satori/go.uuid"
)

const (
	providerType = "sim"
	defaultCount = 10
)

func init() {
	tile.RegisterFactory(&simProviderFactory{})
}

type simProviderFactory struct{}

// Gets provider type key
func (f *simProviderFactory) Type() string {
	return providerType
}

// Create a provider
func (f *simProviderFactory) Create(config tile.Config, manager tile.Manager) (tile.Provider, error) {
	n := tile.GetIntegerDefault(config, "count", defaultCount)

	provider := &simProvider{
		newUID(),
		make([]tile.ID, n),
		manager,
	}

	return provider, nil
}

func newUID() string {
	return uuid.NewV4().String()
}


text:
subdir = src/backend/partitioning
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global

OBJS = partbounds.o partdesc.o partprune.o

include $(top_srcdir)/src/backend/common.mk


text:
// Copyright 2014 The Cockroach Authors.
//
// 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
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// 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. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
// Author: Jiang-Ming Yang (jiangming.yang@gmail.com)
// Author: Tobias Schottdorf (tobias.schottdorf@gmail.com)
// Author: Bram Gruneir (bram+code@cockroachlabs.com)

package storage

import (
	"bytes"
	"crypto/sha1"
	"errors"
	"fmt"
	"math/rand"
	"sync"
	"sync/atomic"
	"time"
	"unsafe"

	"golang.org/x/net/context"

	"github.com/cockroachdb/cockroach/client"
	"github.com/cockroachdb/cockroach/config"
	"github.com/cockroachdb/cockroach/gossip"
	"github.com/cockroachdb/cockroach/keys"
	"github.com/cockroachdb/cockroach/multiraft"
	"github.com/cockroachdb/cockroach/roachpb"
	"github.com/cockroachdb/cockroach/storage/engine"
	"github.com/cockroachdb/cockroach/util"
	"github.com/cockroachdb/cockroach/util/encoding"
	"github.com/cockroachdb/cockroach/util/log"
	"github.com/cockroachdb/cockroach/util/tracer"
	"github.com/gogo/protobuf/proto"
)

const (
	// DefaultHeartbeatInterval is how often heartbeats are sent from the
	// transaction coordinator to a live transaction. These keep it from
	// being preempted by other transactions writing the same keys. If a
	// transaction fails to be heartbeat within 2x the heartbeat interval,
	// it may be aborted by conflicting txns.
	DefaultHeartbeatInterval = 5 * time.Second

	// clusterIDGossipTTL is time-to-live for cluster ID. The cluster ID
	// serves as the sentinel gossip key which informs a node whether or
	// not it's connected to the primary gossip network and not just a
	// partition. As such it must expire on a reasonable basis and be
	// continually re-gossiped. The replica which is the raft leader of
	// the first range gossips it.
	clusterIDGossipTTL = 2 * time.Minute
	// clusterIDGossipInterval is the approximate interval at which the
	// sentinel info is gossiped.
	clusterIDGossipInterval = clusterIDGossipTTL / 2

	// configGossipTTL is the time-to-live for configuration maps.
	configGossipTTL = 0 // does not expire
	// configGossipInterval is the interval at which range leaders gossip
	// their config maps. Even if config maps do not expire, we still
	// need a periodic gossip to safeguard against failure of a leader
	// to gossip after performing an update to the map.
	configGossipInterval = 1 * time.Minute
)

// TestingCommandFilter may be set in tests to intercept the handling
// of commands and artificially generate errors. Return nil to continue
// with regular processing or non-nil to terminate processing with the
// returned error. Note that in a multi-replica test this filter will
// be run once for each replica and must produce consistent results
// each time. Should only be used in tests in the storage and
// storage_test packages.
var TestingCommandFilter func(roachpb.Request, roachpb.Header) error

// This flag controls whether Transaction entries are automatically gc'ed
// upon EndTransaction if they only have local intents (which can be
// resolved synchronously with EndTransaction). Certain tests become
// simpler with this being turned off.
var txnAutoGC = true

// raftInitialLogIndex is the starting point for the raft log. We bootstrap
// the raft membership by synthesizing a snapshot as if there were some
// discarded prefix to the log, so we must begin the log at an arbitrary
// index greater than 1.
const (
	raftInitialLogIndex = 10
	raftInitialLogTerm  = 5

	// DefaultLeaderLeaseDuration is the default duration of the leader lease.
	DefaultLeaderLeaseDuration = time.Second
)

// tsCacheMethods specifies the set of methods which affect the
// timestamp cache. This syntax creates a sparse array with maximum
// index equal to the value of the final Method. Unused indexes
// default to false.
var tsCacheMethods = [...]bool{
	roachpb.Get:                true,
	roachpb.Put:                true,
	roachpb.ConditionalPut:     true,
	roachpb.Increment:          true,
	roachpb.Scan:               true,
	roachpb.ReverseScan:        true,
	roachpb.Delete:             true,
	roachpb.DeleteRange:        true,
	roachpb.ResolveIntent:      true,
	roachpb.ResolveIntentRange: true,
}

// usesTimestampCache returns true if the request affects or is
// affected by the timestamp cache.
func usesTimestampCache(r roachpb.Request) bool {
	m := r.Method()
	if m < 0 || m >= roachpb.Method(len(tsCacheMethods)) {
		return false
	}
	return tsCacheMethods[m]
}

// A pendingCmd holds a done channel for a command sent to Raft. Once
// committed to the Raft log, the command is executed and the result returned
// via the done channel.
type pendingCmd struct {
	ctx  context.Context
	done chan roachpb.ResponseWithError // Used to signal waiting RPC handler
}

type cmdIDKey string

// A Replica is a contiguous keyspace with writes managed via an
// instance of the Raft consensus algorithm. Many ranges may exist
// in a store and they are unlikely to be contiguous. Ranges are
// independent units and are responsible for maintaining their own
// integrity by replacing failed replicas, splitting and merging
// as appropriate.
type Replica struct {
	desc     unsafe.Pointer // Atomic pointer for *roachpb.RangeDescriptor
	store    *Store
	stats    *rangeStats // Range statistics
	maxBytes int64       // Max bytes before split.
	// Last index persisted to the raft log (not necessarily committed).
	// Updated atomically.
	lastIndex uint64
	// Last index applied to the state machine. Updated atomically.
	appliedIndex uint64
	systemDBHash []byte         // sha1 hash of the system config @ last gossip
	lease        unsafe.Pointer // Information for leader lease, updated atomically
	llMu         sync.Mutex     // Synchronizes readers' requests for leader lease
	sequence     *SequenceCache // Provides txn replay protection

	// proposeRaftCommandFn can be set to mock out the propose operation.
	proposeRaftCommandFn func(cmdIDKey, roachpb.RaftCommand) <-chan error

	// Held in read mode during read-only commands. Held in exclusive mode to
	// prevent read-only commands from executing. Acquired before the embedded
	// RWMutex
	readOnlyCmdMu sync.RWMutex

	sync.RWMutex                 // Protects the following fields:
	cmdQ         *CommandQueue   // Enforce at most one command is running per key(s)
	tsCache      *TimestampCache // Most recent timestamps for keys / key ranges
	pendingSeq   uint64          // atomic sequence counter for cmdIDKey generation
	pendingCmds  map[cmdIDKey]*pendingCmd

	// pendingReplica houses a replica that is not yet in the range
	// descriptor, since we must be able to look up a replica's
	// descriptor in order to add it to the range. It is protected by
	// the RWMutex and once it has taken on a non-zero value it must not
	// be changed until that operation has completed and it has been
	// reset to a zero value. The sync.Cond is signaled whenever
	// pendingReplica.value changes to zero.
	pendingReplica struct {
		*sync.Cond
		value roachpb.ReplicaDescriptor
	}
	truncatedState unsafe.Pointer // *roachpb.RaftTruncatedState
}

var _ client.Sender = &Replica{}

// NewReplica initializes the replica using the given metadata.
func NewReplica(desc *roachpb.RangeDescriptor, rm *Store) (*Replica, error) {
	r := &Replica{
		store:       rm,
		cmdQ:        NewCommandQueue(),
		tsCache:     NewTimestampCache(rm.Clock()),
		sequence:    NewSequenceCache(desc.RangeID),
		pendingCmds: map[cmdIDKey]*pendingCmd{},
	}
	r.pendingReplica.Cond = sync.NewCond(r)
	r.setDescWithoutProcessUpdate(desc)

	lastIndex, err := r.loadLastIndex()
	if err != nil {
		return nil, err
	}
	atomic.StoreUint64(&r.lastIndex, lastIndex)

	appliedIndex, err := r.loadAppliedIndex(r.store.Engine())
	if err != nil {
		return nil, err
	}
	atomic.StoreUint64(&r.appliedIndex, appliedIndex)

	lease, err := loadLeaderLease(r.store.Engine(), desc.RangeID)
	if err != nil {
		return nil, err
	}
	atomic.StorePointer(&r.lease, unsafe.Pointer(lease))

	if r.ContainsKey(keys.SystemDBSpan.Key) {
		r.maybeGossipSystemConfig()
	}

	if r.stats, err = newRangeStats(desc.RangeID, rm.Engine()); err != nil {
		return nil, err
	}

	return r, nil
}

// String returns a string representation of the range.
func (r *Replica) String() string {
	desc := r.Desc()
	return fmt.Sprintf("range=%d [%s-%s)", desc.RangeID, desc.StartKey, desc.EndKey)
}

// Destroy cleans up all data associated with this range, leaving a tombstone.
func (r *Replica) Destroy(origDesc roachpb.RangeDescriptor) error {
	desc := r.Desc()
	if _, rd := desc.FindReplica(r.store.StoreID()); rd != nil && rd.ReplicaID >= origDesc.NextReplicaID {
		return util.Errorf("cannot destroy replica %s; replica ID has changed (%s >= %s)",
			r, rd.ReplicaID, origDesc.NextReplicaID)
	}
	iter := newReplicaDataIterator(desc, r.store.Engine())
	defer iter.Close()
	batch := r.store.Engine().NewBatch()
	defer batch.Close()
	for ; iter.Valid(); iter.Next() {
		_ = batch.Clear(iter.Key())
	}

	// Save a tombstone. The range cannot be re-replicated onto this
	// node without having a replica ID of at least desc.NextReplicaID.
	tombstoneKey := keys.RaftTombstoneKey(desc.RangeID)
	tombstone := &roachpb.RaftTombstone{
		NextReplicaID: desc.NextReplicaID,
	}
	if err := engine.MVCCPutProto(batch, nil, tombstoneKey, roachpb.ZeroTimestamp, nil, tombstone); err != nil {
		return err
	}

	return batch.Commit()
}

// context returns a context which is initialized with information about
// this range. It is only relevant when commands need to be executed
// on this range in the absence of a pre-existing context, such as
// during range scanner operations.
func (r *Replica) context() context.Context {
	return context.WithValue(r.store.Context(nil), log.RangeID, r.Desc().RangeID)
}

// GetMaxBytes atomically gets the range maximum byte limit.
func (r *Replica) GetMaxBytes() int64 {
	return atomic.LoadInt64(&r.maxBytes)
}

// SetMaxBytes atomically sets the maximum byte limit before
// split. This value is cached by the range for efficiency.
func (r *Replica) SetMaxBytes(maxBytes int64) {
	atomic.StoreInt64(&r.maxBytes, maxBytes)
}

// IsFirstRange returns true if this is the first range.
func (r *Replica) IsFirstRange() bool {
	return bytes.Equal(r.Desc().StartKey, roachpb.RKeyMin)
}

func loadLeaderLease(eng engine.Engine, rangeID roachpb.RangeID) (*roachpb.Lease, error) {
	lease := &roachpb.Lease{}
	if _, err := engine.MVCCGetProto(eng, keys.RaftLeaderLeaseKey(rangeID), roachpb.ZeroTimestamp, true, nil, lease); err != nil {
		return nil, err
	}
	return lease, nil
}

// getLease returns the current leader lease.
func (r *Replica) getLease() *roachpb.Lease {
	return (*roachpb.Lease)(atomic.LoadPointer(&r.lease))
}

// newNotLeaderError returns a NotLeaderError initialized with the
// replica for the holder (if any) of the given lease.
func (r *Replica) newNotLeaderError(l *roachpb.Lease, originStoreID roachpb.StoreID) error {
	err := &roachpb.NotLeaderError{}
	if l != nil && l.Replica.ReplicaID != 0 {
		desc := r.Desc()

		err.RangeID = desc.RangeID
		_, err.Replica = desc.FindReplica(originStoreID)
		_, err.Leader = desc.FindReplica(l.Replica.StoreID)
	}
	return err
}

// requestLeaderLease sends a request to obtain or extend a leader lease for
// this replica. Unless an error is returned, the obtained lease will be valid
// for a time interval containing the requested timestamp.
func (r *Replica) requestLeaderLease(timestamp roachpb.Timestamp) error {
	// TODO(Tobias): get duration from configuration, either as a config flag
	// or, later, dynamically adjusted.
	duration := DefaultLeaderLeaseDuration

	// Prepare a Raft command to get a leader lease for this replica.
	expiration := timestamp.Add(int64(duration), 0)
	desc := r.Desc()
	_, replica := desc.FindReplica(r.store.StoreID())
	if replica == nil {
		return roachpb.NewRangeNotFoundError(desc.RangeID)
	}
	args := &roachpb.LeaderLeaseRequest{
		Span: roachpb.Span{
			Key: desc.StartKey.AsRawKey(),
		},
		Lease: roachpb.Lease{
			Start:      timestamp,
			Expiration: expiration,
			Replica:    *replica,
		},
	}
	ba := roachpb.BatchRequest{}
	ba.RangeID = desc.RangeID
	ba.Add(args)

	// The raft command becomes moot after its expiration, so give it a
	// context with a deadline.
	// We compute a new deadline here using time.Now() instead of using
	// expiration.GoTime() because in many tests database time uses
	// a fake clock.
	ctx, cancel := context.WithDeadline(r.context(), time.Now().Add(duration))
	defer cancel()

	// Send lease request directly to raft in order to skip unnecessary
	// checks from normal request machinery, (e.g. the command queue).
	// Note that the command itself isn't traced, but usually the caller
	// waiting for the result has an active Trace.
	errChan, pendingCmd := r.proposeRaftCommand(ctx, ba)

	select {
	case err := <-errChan:
		if err != nil {
			return err
		}
	case <-ctx.Done():
		// If the context expired we don't know who got the lease but we
		// know we didn't.
		return r.newNotLeaderError(nil, r.store.StoreID())
	}
	// Next if the command was committed, wait for the range to apply it.
	select {
	case c := <-pendingCmd.done:
		return c.Err
	case <-ctx.Done():
		return r.newNotLeaderError(nil, r.store.StoreID())
	}
}

// redirectOnOrAcquireLeaderLease checks whether this replica has the
// leader lease at the specified timestamp. If it does, returns
// success. If another replica currently holds the lease, redirects by
// returning NotLeaderError. If the lease is expired, a renewal is
// synchronously requested. This method uses the leader lease mutex
// to guarantee only one request to grant the lease is pending.
//
// TODO(spencer): implement threshold regrants to avoid latency in
//  the presence of read or write pressure sufficiently close to the
//  current lease's expiration.
//
// TODO(spencer): for write commands, don't wait while requesting
//  the leader lease. If the lease acquisition fails, the write cmd
//  will fail as well. If it succeeds, as is likely, then the write
//  will not incur latency waiting for the command to complete.
//  Reads, however, must wait.
func (r *Replica) redirectOnOrAcquireLeaderLease(trace *tracer.Trace, timestamp roachpb.Timestamp) error {
	r.llMu.Lock()
	defer r.llMu.Unlock()

	if lease := r.getLease(); lease.Covers(timestamp) {
		if lease.OwnedBy(r.store.StoreID()) {
			// Happy path: We have an active lease, nothing to do.
			return nil
		}
		// If lease is currently held by another, redirect to holder.
		return r.newNotLeaderError(lease, r.store.StoreID())
	}
	defer trace.Epoch("request leader lease")()
	// Otherwise, no active lease: Request renewal.
	err := r.requestLeaderLease(timestamp)

	// Getting a LeaseRejectedError back means someone else got there first, or
	// the lease request was somehow invalid due to a concurrent change.
	//
	// In the case where another machine obtained the lease, we are certain that
	// it can't be this replica because we're holding a lock.
	//
	// In all cases, the error is converted to a NotLeaderError.
	if _, ok := err.(*roachpb.LeaseRejectedError); ok {
		lease := r.getLease()
		if !lease.Covers(timestamp) {
			// The lease was rejected even though it was not obtained by another
			// replica.
			if log.V(1) {
				log.Warningf("Lease for range %s rejected at timestamp %v: %s",
					r, timestamp, err)
			}
			lease = nil
		}
		return r.newNotLeaderError(lease, r.store.StoreID())
	}
	return err
}

// isInitialized is true if we know the metadata of this range, either
// because we created it or we have received an initial snapshot from
// another node. It is false when a range has been created in response
// to an incoming message but we are waiting for our initial snapshot.
func (r *Replica) isInitialized() bool {
	return len(r.Desc().EndKey) > 0
}

// Desc atomically returns the range's descriptor.
func (r *Replica) Desc() *roachpb.RangeDescriptor {
	return (*roachpb.RangeDescriptor)(atomic.LoadPointer(&r.desc))
}

// setDesc atomically sets the range's descriptor. This method calls
// processRangeDescriptorUpdate() to make the range manager handle the
// descriptor update.
func (r *Replica) setDesc(desc *roachpb.RangeDescriptor) error {
	r.setDescWithoutProcessUpdate(desc)
	if r.store == nil {
		// r.rm is null in some tests.
		return nil
	}
	return r.store.processRangeDescriptorUpdate(r)
}

// setDescWithoutProcessUpdate updates the range descriptor without calling
// processRangeDescriptorUpdate.
func (r *Replica) setDescWithoutProcessUpdate(desc *roachpb.RangeDescriptor) {
	atomic.StorePointer(&r.desc, unsafe.Pointer(desc))
}

// getCachedTruncatedState atomically returns the range's cached truncated
// state.
func (r *Replica) getCachedTruncatedState() *roachpb.RaftTruncatedState {
	return (*roachpb.RaftTruncatedState)(atomic.LoadPointer(&r.truncatedState))
}

// setCachedTruncatedState atomically sets the range's truncated state.
func (r *Replica) setCachedTruncatedState(state *roachpb.RaftTruncatedState) {
	atomic.StorePointer(&r.truncatedState, unsafe.Pointer(state))
}

// GetReplica returns the replica for this range from the range descriptor.
// Returns nil if the replica is not found.
func (r *Replica) GetReplica() *roachpb.ReplicaDescriptor {
	_, replica := r.Desc().FindReplica(r.store.StoreID())
	return replica
}

// ReplicaDescriptor returns information about the given member of this replica's range.
func (r *Replica) ReplicaDescriptor(replicaID roachpb.ReplicaID) (roachpb.ReplicaDescriptor, error) {
	r.RLock()
	defer r.RUnlock()

	desc := r.Desc()
	for _, repAddress := range desc.Replicas {
		if repAddress.ReplicaID == replicaID {
			return repAddress, nil
		}
	}
	if r.pendingReplica.value.ReplicaID == replicaID {
		return r.pendingReplica.value, nil
	}
	return roachpb.ReplicaDescriptor{}, util.Errorf("replica %d not found in range %d",
		replicaID, desc.RangeID)
}

// GetMVCCStats returns a copy of the MVCC stats object for this range.
func (r *Replica) GetMVCCStats() engine.MVCCStats {
	return r.stats.GetMVCC()
}

// ContainsKey returns whether this range contains the specified key.
func (r *Replica) ContainsKey(key roachpb.Key) bool {
	return containsKey(*r.Desc(), key)
}

func containsKey(desc roachpb.RangeDescriptor, key roachpb.Key) bool {
	return desc.ContainsKey(keys.Addr(key))
}

// ContainsKeyRange returns whether this range contains the specified
// key range from start to end.
func (r *Replica) ContainsKeyRange(start, end roachpb.Key) bool {
	return containsKeyRange(*r.Desc(), start, end)
}

func containsKeyRange(desc roachpb.RangeDescriptor, start, end roachpb.Key) bool {
	return desc.ContainsKeyRange(keys.Addr(start), keys.Addr(end))
}

// GetGCMetadata reads the latest GC metadata for this range.
func (r *Replica) GetGCMetadata() (*roachpb.GCMetadata, error) {
	key := keys.RangeGCMetadataKey(r.Desc().RangeID)
	gcMeta := &roachpb.GCMetadata{}
	_, err := engine.MVCCGetProto(r.store.Engine(), key, roachpb.ZeroTimestamp, true, nil, gcMeta)
	if err != nil {
		return nil, err
	}
	return gcMeta, nil
}

// GetLastVerificationTimestamp reads the timestamp at which the range's
// data was last verified.
func (r *Replica) GetLastVerificationTimestamp() (roachpb.Timestamp, error) {
	key := keys.RangeLastVerificationTimestampKey(r.Desc().RangeID)
	timestamp := roachpb.Timestamp{}
	_, err := engine.MVCCGetProto(r.store.Engine(), key, roachpb.ZeroTimestamp, true, nil, &timestamp)
	if err != nil {
		return roachpb.ZeroTimestamp, err
	}
	return timestamp, nil
}

// SetLastVerificationTimestamp writes the timestamp at which the range's
// data was last verified.
func (r *Replica) SetLastVerificationTimestamp(timestamp roachpb.Timestamp) error {
	key := keys.RangeLastVerificationTimestampKey(r.Desc().RangeID)
	return engine.MVCCPutProto(r.store.Engine(), nil, key, roachpb.ZeroTimestamp, nil, &timestamp)
}

// Send adds a command for execution on this range. The command's
// affected keys are verified to be contained within the range and the
// range's leadership is confirmed. The command is then dispatched
// either along the read-only execution path or the read-write Raft
// command queue.
// TODO(tschottdorf): use BatchRequest w/o pointer receiver.
func (r *Replica) Send(ctx context.Context, ba roachpb.BatchRequest) (*roachpb.BatchResponse, *roachpb.Error) {
	var br *roachpb.BatchResponse
	var err error

	if err := r.checkBatchRequest(ba); err != nil {
		return nil, roachpb.NewError(err)
	}

	// TODO(tschottdorf) Some (internal) requests go here directly, so they
	// won't be traced.
	trace := tracer.FromCtx(ctx)
	// Differentiate between admin, read-only and write.
	if ba.IsAdmin() {
		defer trace.Epoch("admin path")()
		br, err = r.addAdminCmd(ctx, ba)
	} else if ba.IsReadOnly() {
		defer trace.Epoch("read-only path")()
		br, err = r.addReadOnlyCmd(ctx, ba)
	} else if ba.IsWrite() {
		defer trace.Epoch("read-write path")()
		br, err = r.addWriteCmd(ctx, ba, nil)
	} else if len(ba.Requests) == 0 {
		// empty batch; shouldn't happen (we could handle it, but it hints
		// at someone doing weird things, and once we drop the key range
		// from the header it won't be clear how to route those requests).
		panic("empty batch")
	} else {
		panic(fmt.Sprintf("don't know how to handle command %s", ba))
	}
	if err == multiraft.ErrGroupDeleted {
		// This error needs to be converted appropriately so that
		// clients will retry.
		err = roachpb.NewRangeNotFoundError(r.Desc().RangeID)
	}
	// TODO(tschottdorf): assert nil reply on error.
	if err != nil {
		trace.SetError()
		trace.Event(fmt.Sprintf("error: %s", err))
		return nil, roachpb.NewError(err)
	}
	return br, nil
}

// TODO(tschottdorf): almost obsolete.
func (r *Replica) checkCmdHeader(header *roachpb.Span) error {
	if !r.ContainsKeyRange(header.Key, header.EndKey) {
		return roachpb.NewRangeKeyMismatchError(header.Key, header.EndKey, r.Desc())
	}
	return nil
}

// checkBatchRequest verifies BatchRequest validity requirements. In
// particular, timestamp, user, user priority and transactions must
// all be set to identical values between the batch request header and
// all constituent batch requests. Also, either all requests must be
// read-only, or none.
// TODO(tschottdorf): should check that request is contained in range
// and that EndTransaction only occurs at the very end.
func (r *Replica) checkBatchRequest(ba roachpb.BatchRequest) error {
	if ba.IsReadOnly() {
		if ba.ReadConsistency == roachpb.INCONSISTENT && ba.Txn != nil {
			// Disallow any inconsistent reads within txns.
			return util.Errorf("cannot allow inconsistent reads within a transaction")
		}
		if ba.ReadConsistency == roachpb.CONSENSUS {
			return util.Errorf("consensus reads not implemented")
		}
	} else if ba.ReadConsistency == roachpb.INCONSISTENT {
		return util.Errorf("inconsistent mode is only available to reads")
	}

	return nil
}

// beginCmds waits for any overlapping, already-executing commands via
// the command queue and adds itself to queues based on keys affected by the
// batched commands. This gates subsequent commands with overlapping keys or
// key ranges. This method will block if there are any overlapping commands
// already in the queue. Returns the command queue insertion keys, to be
// supplied to a subsequent invocation of endCmds().
func (r *Replica) beginCmds(ba *roachpb.BatchRequest) ([]interface{}, error) {
	var cmdKeys []interface{}
	// Don't use the command queue for inconsistent reads.
	if ba.ReadConsistency != roachpb.INCONSISTENT {
		r.Lock()
		var wg sync.WaitGroup
		var spans []roachpb.Span
		readOnly := ba.IsReadOnly()
		for _, union := range ba.Requests {
			h := union.GetInner().Header()
			spans = append(spans, roachpb.Span{Key: h.Key, EndKey: h.EndKey})
		}
		r.cmdQ.GetWait(readOnly, &wg, spans...)
		cmdKeys = append(cmdKeys, r.cmdQ.Add(readOnly, spans...)...)
		r.Unlock()
		wg.Wait()
	}

	// Update the incoming timestamp if unset. Wait until after any
	// preceding command(s) for key range are complete so that the node
	// clock has been updated to the high water mark of any commands
	// which might overlap this one in effect.
	if ba.Timestamp.Equal(roachpb.ZeroTimestamp) {
		if ba.Txn != nil {
			// TODO(tschottdorf): see if this is already done somewhere else.
			ba.Timestamp = ba.Txn.Timestamp
		} else {
			ba.Timestamp = r.store.Clock().Now()
		}
	}

	return cmdKeys, nil
}

// endCmds removes pending commands from the command queue and updates
// the timestamp cache using the final timestamp of each command.
func (r *Replica) endCmds(cmdKeys []interface{}, ba roachpb.BatchRequest, err error) {
	r.Lock()
	// Only update the timestamp cache if the command succeeded and we're not
	// doing inconsistent ops (in which case the ops are always read-only).
	if err == nil && ba.ReadConsistency != roachpb.INCONSISTENT {
		for _, union := range ba.Requests {
			args := union.GetInner()
			if usesTimestampCache(args) {
				header := args.Header()
				var txnID []byte
				if ba.Txn != nil {
					txnID = ba.Txn.ID
				}
				r.tsCache.Add(header.Key, header.EndKey, ba.Timestamp, txnID, roachpb.IsReadOnly(args))
			}
		}
	}
	r.cmdQ.Remove(cmdKeys)
	r.Unlock()
}

// addAdminCmd executes the command directly. There is no interaction
// with the command queue or the timestamp cache, as admin commands
// are not meant to consistently access or modify the underlying data.
// Admin commands must run on the leader replica. Batch support here is
// limited to single-element batches; everything else catches an error.
func (r *Replica) addAdminCmd(ctx context.Context, ba roachpb.BatchRequest) (*roachpb.BatchResponse, error) {
	if len(ba.Requests) != 1 {
		return nil, util.Errorf("only single-element admin batches allowed")
	}
	args := ba.Requests[0].GetInner()

	if err := r.checkCmdHeader(args.Header()); err != nil {
		return nil, err
	}

	// Admin commands always require the leader lease.
	if err := r.redirectOnOrAcquireLeaderLease(tracer.FromCtx(ctx), ba.Timestamp); err != nil {
		return nil, err
	}

	var resp roachpb.Response
	var err error
	switch tArgs := args.(type) {
	case *roachpb.AdminSplitRequest:
		var reply roachpb.AdminSplitResponse
		reply, err = r.AdminSplit(*tArgs, r.Desc())
		resp = &reply
	case *roachpb.AdminMergeRequest:
		var reply roachpb.AdminMergeResponse
		reply, err = r.AdminMerge(*tArgs, r.Desc())
		resp = &reply
	default:
		return nil, util.Errorf("unrecognized admin command: %T", args)
	}

	if err != nil {
		return nil, err
	}
	br := &roachpb.BatchResponse{}
	br.Add(resp)
	br.Timestamp = ba.Timestamp
	br.Txn = resp.Header().Txn
	return br, nil
}

// addReadOnlyCmd updates the read timestamp cache and waits for any
// overlapping writes currently processing through Raft ahead of us to
// clear via the read queue.
func (r *Replica) addReadOnlyCmd(ctx context.Context, ba roachpb.BatchRequest) (*roachpb.BatchResponse, error) {
	header := ba.Header
	trace := tracer.FromCtx(ctx)

	// Add the read to the command queue to gate subsequent
	// overlapping commands until this command completes.
	qDone := trace.Epoch("command queue")
	cmdKeys, err := r.beginCmds(&ba)
	qDone()
	if err != nil {
		return nil, err
	}

	// If there are command keys (there might not be if reads are
	// inconsistent), the read requires the leader lease.
	if len(cmdKeys) > 0 {
		if err := r.redirectOnOrAcquireLeaderLease(trace, header.Timestamp); err != nil {
			r.endCmds(cmdKeys, ba, err)
			return nil, err
		}
	}

	r.readOnlyCmdMu.RLock()
	// Execute read-only batch command. It checks for matching key range; note
	// that holding readMu throughout is important to avoid reads from the
	// "wrong" key range being served after the range has been split.
	br, intents, err := r.executeBatch(r.store.Engine(), nil, ba)

	if err == nil && ba.Txn != nil {
		// Checking the sequence cache on reads makes sure that when our
		// transaction has already been aborted, we don't experience anomalous
		// conditions as described in #2231.
		err = r.checkSequenceCache(r.store.Engine(), *ba.Txn)
	}
	r.handleSkippedIntents(intents)

	// Remove keys from command queue.
	r.endCmds(cmdKeys, ba, err)
	// Important to unlock only here to capture the timestamp cache update.
	r.readOnlyCmdMu.RUnlock()

	if err != nil {
		return nil, err
	}
	return br, nil
}

// addWriteCmd first adds the keys affected by this command as pending writes
// to the command queue. Next, the timestamp cache is checked to determine if
// any newer accesses to this command's affected keys have been made. If so,
// the command's timestamp is moved forward. Finally, the command is submitted
// to Raft. Upon completion, the write is removed from the read queue and any
// error returned. If a WaitGroup is supplied, it is signaled when the command
// enters Raft or the function returns with a preprocessing error, whichever
// happens earlier.
func (r *Replica) addWriteCmd(ctx context.Context, ba roachpb.BatchRequest, wg *sync.WaitGroup) (*roachpb.BatchResponse, error) {
	signal := func() {
		if wg != nil {
			wg.Done()
			wg = nil
		}
	}

	// This happens more eagerly below, but it's important to guarantee that
	// early returns do not skip this.
	defer signal()

	trace := tracer.FromCtx(ctx)

	// Add the write to the command queue to gate subsequent overlapping
	// commands until this command completes. Note that this must be
	// done before getting the max timestamp for the key(s), as
	// timestamp cache is only updated after preceding commands have
	// been run to successful completion.
	qDone := trace.Epoch("command queue")
	cmdKeys, err := r.beginCmds(&ba)
	qDone()
	if err != nil {
		return nil, err
	}

	// This replica must have leader lease to process a write.
	if err := r.redirectOnOrAcquireLeaderLease(trace, ba.Timestamp); err != nil {
		r.endCmds(cmdKeys, ba, err)
		return nil, err
	}

	// Two important invariants of Cockroach: 1) encountering a more
	// recently written value means transaction restart. 2) values must
	// be written with a greater timestamp than the most recent read to
	// the same key. Check the timestamp cache for reads/writes which
	// are at least as recent as the timestamp of this write. For
	// writes, send WriteTooOldError; for reads, update the write's
	// timestamp. When the write returns, the updated timestamp will
	// inform the final commit timestamp.
	//
	// Find the maximum timestamp required to satisfy all requests in
	// the batch and then apply that to all requests.
	r.Lock()
	for _, union := range ba.Requests {
		args := union.GetInner()
		if usesTimestampCache(args) {
			header := args.Header()
			var txnID []byte
			if ba.Txn != nil {
				txnID = ba.Txn.ID
			}
			rTS, wTS := r.tsCache.GetMax(header.Key, header.EndKey, txnID)

			// Always push the timestamp forward if there's been a read which
			// occurred after our txn timestamp.
			if !rTS.Less(ba.Timestamp) {
				ba.Timestamp = rTS.Next()
			}
			// If there's a newer write timestamp...
			if !wTS.Less(ba.Timestamp) {
				// If we're in a txn, we still go ahead and try the write since
				// we want to avoid restarting the transaction in the event that
				// there isn't an intent or the intent can be pushed by us.
				//
				// If we're not in a txn, it's trivial to just advance our timestamp.
				if ba.Txn == nil {
					ba.Timestamp = wTS.Next()
				}
			}
		}
	}

	r.Unlock()

	defer trace.Epoch("raft")()

	errChan, pendingCmd := r.proposeRaftCommand(ctx, ba)

	signal()

	// First wait for raft to commit or abort the command.
	var br *roachpb.BatchResponse
	if err = <-errChan; err == nil {
		// Next if the command was committed, wait for the range to apply it.
		respWithErr := <-pendingCmd.done
		br, err = respWithErr.Reply, respWithErr.Err
	}

	r.endCmds(cmdKeys, ba, err)
	return br, err
}

// proposeRaftCommand prepares necessary pending command struct and
// initializes a client command ID if one hasn't been. It then
// proposes the command to Raft and returns the error channel and
// pending command struct for receiving.
func (r *Replica) proposeRaftCommand(ctx context.Context, ba roachpb.BatchRequest) (<-chan error, *pendingCmd) {
	pendingCmd := &pendingCmd{
		ctx:  ctx,
		done: make(chan roachpb.ResponseWithError, 1),
	}
	desc := r.Desc()
	_, replica := desc.FindReplica(r.store.StoreID())
	if replica == nil {
		errChan := make(chan error, 1)
		errChan <- roachpb.NewRangeNotFoundError(desc.RangeID)
		return errChan, pendingCmd
	}
	raftCmd := roachpb.RaftCommand{
		RangeID:       desc.RangeID,
		OriginReplica: *replica,
		Cmd:           ba,
	}

	buf := make([]byte, 0, multiraft.CommandIDLen)
	{
		buf = encoding.EncodeUint64(buf, uint64(rand.Int63()))[:multiraft.CommandIDLen]
	}
	idKey := cmdIDKey(buf)

	r.Lock()
	r.pendingCmds[idKey] = pendingCmd
	r.Unlock()

	var errChan <-chan error
	if r.proposeRaftCommandFn != nil {
		errChan = r.proposeRaftCommandFn(idKey, raftCmd)
	} else {
		errChan = r.store.ProposeRaftCommand(idKey, raftCmd)
	}

	return errChan, pendingCmd
}

// processRaftCommand processes a raft command by unpacking the command
// struct to get args and reply and then applying the command to the
// state machine via applyRaftCommand(). The error result is sent on
// the command's done channel, if available.
func (r *Replica) processRaftCommand(idKey cmdIDKey, index uint64, raftCmd roachpb.RaftCommand) error {
	if index == 0 {
		log.Fatalc(r.context(), "processRaftCommand requires a non-zero index")
	}

	r.Lock()
	cmd := r.pendingCmds[idKey]
	delete(r.pendingCmds, idKey)
	r.Unlock()

	var ctx context.Context
	if cmd != nil {
		// We initiated this command, so use the caller-supplied context.
		ctx = cmd.ctx
	} else {
		// TODO(tschottdorf): consider the Trace situation here.
		ctx = r.context()
	}

	trace := tracer.FromCtx(ctx)
	execDone := trace.Epoch("applying batch")
	// applyRaftCommand will return "expected" errors, but may also indicate
	// replica corruption (as of now, signaled by a replicaCorruptionError).
	// We feed its return through maybeSetCorrupt to act when that happens.
	br, err := r.applyRaftCommand(ctx, index, raftCmd.OriginReplica, raftCmd.Cmd)
	err = r.maybeSetCorrupt(err)
	execDone()

	if cmd != nil {
		cmd.done <- roachpb.ResponseWithError{Reply: br, Err: err}
	} else if err != nil && log.V(1) {
		log.Errorc(r.context(), "error executing raft command: %s", err)
	}

	return err
}

// applyRaftCommand applies a raft command from the replicated log to the
// underlying state machine (i.e. the engine).
// When certain critical operations fail, a replicaCorruptionError may be
// returned and must be handled by the caller.
func (r *Replica) applyRaftCommand(ctx context.Context, index uint64, originReplica roachpb.ReplicaDescriptor,
	ba roachpb.BatchRequest) (*roachpb.BatchResponse, error) {
	if index <= 0 {
		log.Fatalc(ctx, "raft command index is <= 0")
	}

	// If we have an out of order index, there's corruption. No sense in trying
	// to update anything or run the command. Simply return a corruption error.
	if oldIndex := atomic.LoadUint64(&r.appliedIndex); oldIndex >= index {
		return nil, newReplicaCorruptionError(util.Errorf("applied index moved backwards: %d >= %d", oldIndex, index))
	}

	// Call the helper, which returns a batch containing data written
	// during command execution and any associated error.
	ms := engine.MVCCStats{}
	batch, br, intents, rErr := r.applyRaftCommandInBatch(ctx, index, originReplica, ba, &ms)
	defer batch.Close()

	// Advance the last applied index and commit the batch.
	if err := setAppliedIndex(batch, r.Desc().RangeID, index); err != nil {
		log.Fatalc(ctx, "setting applied index in a batch should never fail: %s", err)
	}
	if err := batch.Commit(); err != nil {
		rErr = newReplicaCorruptionError(util.Errorf("could not commit batch"), err, rErr)
	} else {
		// Update cached appliedIndex if we were able to set the applied index on disk.
		atomic.StoreUint64(&r.appliedIndex, index)
		// Invalidate the cache and let raftTruncatedState() read the value the next
		// time it's required.
		if _, ok := ba.GetArg(roachpb.TruncateLog); ok {
			r.setCachedTruncatedState(nil)
		}
	}

	// On successful write commands, flush to event feed, and handle other
	// write-related triggers including splitting and config gossip updates.
	if rErr == nil && ba.IsWrite() {
		// Publish update to event feed.
		// TODO(spencer): we should be sending feed updates for each part
		// of the batch. In particular, stats should be reported per-command.
		r.store.EventFeed().updateRange(r, roachpb.Batch, &ms)
		// If the commit succeeded, potentially add range to split queue.
		r.maybeAddToSplitQueue()
	}

	// On the replica on which this command originated, resolve skipped intents
	// asynchronously - even on failure.
	if originReplica.StoreID == r.store.StoreID() {
		r.handleSkippedIntents(intents)
	}

	return br, rErr
}

// applyRaftCommandInBatch executes the command in a batch engine and
// returns the batch containing the results. The caller is responsible
// for committing the batch, even on error.
func (r *Replica) applyRaftCommandInBatch(ctx context.Context, index uint64, originReplica roachpb.ReplicaDescriptor,
	ba roachpb.BatchRequest, ms *engine.MVCCStats) (engine.Engine, *roachpb.BatchResponse, []intentsWithArg, error) {
	// Create a new batch for the command to ensure all or nothing semantics.
	btch := r.store.Engine().NewBatch()

	// Check the sequence for this batch. Only applies to transactional
	// requests: on a cache hit, the transaction is instructed to restart.
	// The epoch check allows us to poison the sequence cache to avoid
	// anomalies when we know that the transaction has been aborted or pushed,
	// but it itself does not.
	if ba.IsWrite() && ba.Txn != nil {
		if err := r.checkSequenceCache(btch, *ba.Txn); err != nil {
			return btch, nil, nil, err
		}
	}

	for _, union := range ba.Requests {
		args := union.GetInner()

		// TODO(tschottdorf): shouldn't be in the loop. Currently is because
		// we haven't cleaned up the timestamp handling fully.
		if lease := r.getLease(); args.Method() != roachpb.LeaderLease &&
			(!lease.OwnedBy(originReplica.StoreID) || !lease.Covers(ba.Timestamp)) {
			// Verify the leader lease is held, unless this command is trying to
			// obtain it. Any other Raft command has had the leader lease held
			// by the replica at proposal time, but this may no longer be the case.
			// Corruption aside, the most likely reason is a leadership change (the
			// most recent leader assumes responsibility for all past timestamps as
			// well). In that case, it's not valid to go ahead with the execution:
			// Writes must be aware of the last time the mutated key was read, and
			// since reads are served locally by the lease holder without going
			// through Raft, a read which was not taken into account may have been
			// served. Hence, we must retry at the current leader.
			//
			// It's crucial that we don't update the sequence cache for the
			// error returned below since the request is going to be retried
			// with the same sequence number and would get the distributed
			// sender stuck in an infinite loop, retrieving a stale
			// NotLeaderError over and over again, even when proposing at the
			// correct replica.
			return btch, nil, nil, r.newNotLeaderError(lease, originReplica.StoreID)
		}
	}

	// Execute the commands. If this returns without an error, the batch must
	// be committed (EndTransaction with a CommitTrigger may unlock
	// readOnlyCmdMu via a batch.Defer).
	br, intents, err := r.executeBatch(btch, ms, ba)

	// Regardless of error, add result to the sequence cache if this is
	// a write method. This must be done as part of the execution of
	// raft commands so that every replica maintains the same responses
	// to continue request idempotence, even if leadership changes.
	if ba.IsWrite() {
		if err == nil {
			// If command was successful, flush the MVCC stats to the batch.
			if err := r.stats.MergeMVCCStats(btch, ms, ba.Timestamp.WallTime); err != nil {
				// TODO(tschottdorf): ReplicaCorruptionError.
				log.Fatalc(ctx, "setting mvcc stats in a batch should never fail: %s", err)
			}
		} else {
			// TODO(tschottdorf): make `nil` acceptable. Corresponds to
			// roachpb.Response{With->Or}Error.
			br = &roachpb.BatchResponse{}
			// Otherwise, reset the batch to clear out partial execution and
			// prepare for the failed sequence cache entry.
			btch.Close()
			btch = r.store.Engine().NewBatch()
		}
		// Only transactional requests have replay protection.
		if ba.Txn != nil {
			if putErr := r.sequence.Put(btch, ba.Txn.ID, ba.Txn.Epoch,
				ba.Txn.Sequence, ba.Txn.Key, ba.Txn.Timestamp, err); putErr != nil {
				// TODO(tschottdorf): ReplicaCorruptionError.
				log.Fatalc(ctx, "putting a sequence cache entry in a batch should never fail: %s", putErr)
			}
		}
	}

	return btch, br, intents, err
}

// checkSequenceCache checks the sequence cache for the given transaction for
// entries newer or of the same age as our current operation's. In case of a
// replay violation or if the transaction has been aborted, transaction retry
// or transaction abort error is returned, respectively.
// checkSequenceCache locks the replica.
func (r *Replica) checkSequenceCache(b engine.Engine, txn roachpb.Transaction) error {
	r.Lock()
	defer r.Unlock()

	var entry roachpb.SequenceCacheEntry
	if epoch, sequence, readErr := r.sequence.Get(b, txn.ID, &entry); readErr != nil {
		return newReplicaCorruptionError(util.Errorf("could not read from sequence cache"), readErr)
	} else if txn.Epoch > epoch {
		// No cache hit from current or future epoch, continue.
	} else if sequence == roachpb.SequencePoisonAbort {
		// We were poisoned, which means that our Transaction has been
		// aborted and learns about that right now.
		abortErr := roachpb.NewTransactionAbortedError(&txn)
		abortErr.Txn.Timestamp.Forward(entry.Timestamp)
		return abortErr
	} else if sequence >= txn.Sequence || epoch > txn.Epoch {
		// We hit the cache, so let the transaction restart.
		// This is also the path taken by roachpb.SequencePoisonRestart.
		if log.V(1) {
			log.Infof("found sequence cache entry for %s@%d", txn.Short(), txn.Sequence)
		}
		retryErr := roachpb.NewTransactionRetryError(&txn)
		retryErr.Txn.Timestamp.Forward(entry.Timestamp)
		return retryErr
	}
	return nil
}

type intentsWithArg struct {
	args    roachpb.Request
	intents []roachpb.Intent
}

func (r *Replica) executeBatch(batch engine.Engine, ms *engine.MVCCStats, ba roachpb.BatchRequest) (*roachpb.BatchResponse, []intentsWithArg, error) {
	br := &roachpb.BatchResponse{}
	br.Timestamp = ba.Timestamp
	var intents []intentsWithArg
	// If transactional, we use ba.Txn for each individual command and
	// accumulate updates to it.
	isTxn := ba.Txn != nil

	// Ensure that timestamps on individual requests are offset by an incremental
	// amount of logical ticks from the base timestamp of the batch request (if
	// that is non-zero and the batch is not in a Txn). This has the effect of
	// allowing self-overlapping commands within the batch, which would otherwise
	// result in an endless loop of WriteTooOldErrors.
	// TODO(tschottdorf): discuss self-overlap.
	// TODO(tschottdorf): provisional feature to get back to passing tests.
	// Have to discuss how we go about it.
	fiddleWithTimestamps := ba.Txn == nil && ba.IsWrite()

	// TODO(tschottdorf): provisionals ahead. This loop needs to execute each
	// command and propagate txn and timestamp to the next (and, eventually,
	// to the batch response header). We're currently in an intermediate stage
	// in which requests still contain a header with a lot of redundant info,
	// so the code is fairly clumsy and tries to overwrite as much as it can
	// from the batch. There'll certainly be some more iterations to stream-
	// line the code in this loop.
	for index, union := range ba.Requests {
		// Execute the command.
		args := union.GetInner()

		// Set the timestamp for this request.
		ts := ba.Timestamp
		{
			// TODO(tschottdorf): Special exception because of pending
			// self-overlap discussion.
			// TODO(tschottdorf): currently treating PushTxn specially. Otherwise
			// conflict resolution would break because a batch full of pushes of
			// the same Txn for different intents will push the pushee in
			// iteration, bumping up its priority. Unfortunately PushTxn is flagged
			// as a write command (is it really?). Interim solution.
			// Note that being in a txn implies no fiddling.
			if fiddleWithTimestamps && args.Method() != roachpb.PushTxn {
				ts = ba.Timestamp.Add(0, int32(index))
			} else if !isTxn {
				ts = ba.Timestamp
			} else {
				// TODO(tschottdorf): it can happen that the request timestamp
				// is ahead of the Txn timestamp. A few tests do this on pur-
				// pose; should undo that and see if there's a legitimate
				// reason left to keep allowing this behavior.
				ts.Forward(ba.Txn.Timestamp)
			}
		}

		header := ba.Header
		header.Timestamp = ts

		reply, curIntents, err := r.executeCmd(batch, ms, header, args)

		// Collect intents skipped over the course of execution.
		if len(curIntents) > 0 {
			// TODO(tschottdorf): see about refactoring the args away.
			intents = append(intents, intentsWithArg{args: args, intents: curIntents})
		}

		if err != nil {
			if iErr, ok := err.(roachpb.IndexedError); ok {
				iErr.SetErrorIndex(int32(index))
			}
			return nil, intents, err
		}

		// Add the response to the batch, updating the timestamp.
		reply.Header().Timestamp.Forward(ts)
		br.Timestamp.Forward(ts)
		br.Add(reply)
		if isTxn {
			if txn := reply.Header().Txn; txn != nil {
				ba.Txn.Update(txn)
			}
			// The transaction timestamp can't lag behind the actual timestamps
			// we're operating at.
			// TODO(tschottdorf): without this, TestSingleKey fails (since the Txn
			// will commit with its original timestamp). We should have a
			// non-acceptance test verifying this as well.
			// TODO(tschottdorf): wasn't there another place that did this?
			ba.Txn.Timestamp.Forward(br.Timestamp)
		}
	}
	// If transactional, send out the final transaction entry with the reply.
	if isTxn {
		br.Txn = ba.Txn
		// If this is the beginning of the write portion of a transaction,
		// mark the returned transaction as Writing. It's important that
		// we do this only at the end, since updating at the first write
		// could "poison" the transaction on restartable errors, see #2920.
		if _, ok := ba.GetArg(roachpb.BeginTransaction); ok {
			br.Txn.Writing = true
		}
	}
	return br, intents, nil
}

// getLeaseForGossip tries to obtain a leader lease. Only one of the replicas
// should gossip; the bool returned indicates whether it's us.
func (r *Replica) getLeaseForGossip(ctx context.Context) (bool, error) {
	// If no Gossip available (some tests) or range too fresh, noop.
	if r.store.Gossip() == nil || !r.isInitialized() {
		return false, util.Errorf("no gossip or range not initialized")
	}
	var hasLease bool
	var err error
	if !r.store.Stopper().RunTask(func() {
		timestamp := r.store.Clock().Now()
		// Check for or obtain the lease, if none active.
		err = r.redirectOnOrAcquireLeaderLease(tracer.FromCtx(ctx), timestamp)
		hasLease = err == nil
		if err != nil {
			switch e := err.(type) {
			// NotLeaderError means there is an active lease, leaseRejectedError
			// means we tried to get one but someone beat us to it.
			case *roachpb.NotLeaderError, *roachpb.LeaseRejectedError:
				err = nil
			default:
				// Any other error is worth being logged visibly.
				log.Warningc(ctx, "could not acquire lease for range gossip: %s", e)
			}
		}
	}) {
		err = util.Errorf("node is stopping")
	}
	return hasLease, err
}

// maybeGossipFirstRange adds the sentinel and first range metadata to gossip
// if this is the first range and a leader lease can be obtained. The Store
// calls this periodically on first range replicas.
func (r *Replica) maybeGossipFirstRange() error {
	if !r.IsFirstRange() {
		return nil
	}

	ctx := r.context()

	desc := r.Desc()

	// Gossip the cluster ID from all replicas of the first range.
	if log.V(1) {
		log.Infoc(ctx, "gossiping cluster id %s from store %d, range %d", r.store.ClusterID(),
			r.store.StoreID(), r.Desc().RangeID)
	}
	if err := r.store.Gossip().AddInfo(gossip.KeyClusterID, []byte(r.store.ClusterID()), clusterIDGossipTTL); err != nil {
		log.Errorc(ctx, "failed to gossip cluster ID: %s", err)
	}

	if ok, err := r.getLeaseForGossip(ctx); !ok || err != nil {
		return err
	}
	if log.V(1) {
		log.Infoc(ctx, "gossiping sentinel from store %d, range %d", r.store.StoreID(), desc.RangeID)
	}
	if err := r.store.Gossip().AddInfo(gossip.KeySentinel, []byte(r.store.ClusterID()), clusterIDGossipTTL); err != nil {
		log.Errorc(ctx, "failed to gossip sentinel: %s", err)
	}
	if log.V(1) {
		log.Infoc(ctx, "gossiping first range from store %d, range %d", r.store.StoreID(), desc.RangeID)
	}
	if err := r.store.Gossip().AddInfoProto(gossip.KeyFirstRangeDescriptor, desc, configGossipTTL); err != nil {
		log.Errorc(ctx, "failed to gossip first range metadata: %s", err)
	}
	return nil
}

// maybeGossipSystemConfig scans the entire SystemDB span and gossips it.
// The first call is on NewReplica. Further calls come from the trigger
// on an EndTransactionRequest.
//
// Note that maybeGossipSystemConfig gossips information only when the
// lease is actually held. The method does not request a leader lease
// here since LeaderLease and applyRaftCommand call the method and we
// need to avoid deadlocking in redirectOnOrObtainLeaderLease.
// TODO(tschottdorf): Can possibly simplify.
func (r *Replica) maybeGossipSystemConfig() {
	r.Lock()
	defer r.Unlock()
	r.maybeGossipSystemConfigLocked()
}

func (r *Replica) maybeGossipSystemConfigLocked() {
	if r.store.Gossip() == nil || !r.isInitialized() {
		return
	}

	if lease := r.getLease(); !lease.OwnedBy(r.store.StoreID()) || !lease.Covers(r.store.Clock().Now()) {
		// Do not gossip when a leader lease is not held.
		return
	}

	ctx := r.context()
	// TODO(marc): check for bad split in the middle of the SystemDB span.
	kvs, hash, err := r.loadSystemDBSpan()
	if err != nil {
		log.Errorc(ctx, "could not load SystemDB span: %s", err)
		return
	}
	if bytes.Equal(r.systemDBHash, hash) {
		return
	}

	if log.V(1) {
		log.Infoc(ctx, "gossiping system config from store %d, range %d", r.store.StoreID(), r.Desc().RangeID)
	}

	cfg := &config.SystemConfig{Values: kvs}
	if err := r.store.Gossip().AddInfoProto(gossip.KeySystemConfig, cfg, 0); err != nil {
		log.Errorc(ctx, "failed to gossip system config: %s", err)
		return
	}

	// Successfully gossiped. Update tracking hash.
	r.systemDBHash = hash
}

func (r *Replica) handleSkippedIntents(intents []intentsWithArg) {
	if len(intents) == 0 {
		return
	}
	now := r.store.Clock().Now()
	ctx := r.context()
	stopper := r.store.Stopper()

	for _, item := range intents {
		// TODO(tschottdorf): avoid data race related to batch unrolling in ExecuteCmd;
		// can probably go again when that provisional code there is gone. Should
		// still be careful though, a retry could happen and race with args.
		args := proto.Clone(item.args).(roachpb.Request)
		stopper.RunAsyncTask(func() {
			h := roachpb.Header{Timestamp: now}
			resolveIntents, err := r.store.resolveWriteIntentError(ctx, &roachpb.WriteIntentError{
				Intents: item.intents,
			}, r, args, h, roachpb.CLEANUP_TXN)
			if wiErr, ok := err.(*roachpb.WriteIntentError); !ok || wiErr == nil || !wiErr.Resolved {
				log.Warningc(ctx, "failed to push during intent resolution: %s", err)
				return
			}
			if err := r.resolveIntents(ctx, resolveIntents, true /* wait */, true /* poison */); err != nil {
				log.Warningc(ctx, "failed to resolve intents: %s", err)
				return
			}
			// We successfully resolved the intents, so we're able to GC from
			// the txn/sequence span directly.
			// TODO(tschottdorf): down the road, can probably unclog the system
			// here by batching up a bunch of those GCRequests before proposing.
			if args.Method() == roachpb.EndTransaction {
				var ba roachpb.BatchRequest
				txn := item.intents[0].Txn
				gcArgs := roachpb.GCRequest{
					Span: roachpb.Span{Key: r.Desc().StartKey.AsRawKey()},
				}
				{
					kvs, err := r.sequence.GetAllTransactionID(r.store.Engine(), txn.ID)
					if err != nil {
						panic(err) // TODO(tschottdorf): ReplicaCorruptionError
					}
					// Allocate slots for the transaction key and the sequence
					// cache keys.
					gcArgs.Keys = make([]roachpb.GCRequest_GCKey, 0, len(kvs)+1)
					for _, kv := range kvs {
						gcArgs.Keys = append(gcArgs.Keys, roachpb.GCRequest_GCKey{Key: kv.Key})
					}
				}
				gcArgs.Keys = append(gcArgs.Keys, roachpb.GCRequest_GCKey{Key: keys.TransactionKey(txn.Key, txn.ID)})

				ba.Add(&gcArgs)
				if _, pErr := r.addWriteCmd(ctx, ba, nil /* nil */); pErr != nil {
					log.Warningf("could not GC completed transaction: %s", pErr)
				}
			}
		})
	}
}

// TODO(spencerkimball): move to util.
type chainedError struct {
	error
	cause *chainedError
}

// Error implements the error interface, printing the underlying chain of errors.
func (ce *chainedError) Error() string {
	if ce == nil {
		ce = &chainedError{}
	}
	if ce.cause != nil {
		return fmt.Sprintf("%s (caused by %s)", ce.error, ce.cause)
	}
	return ce.error.Error()
}

// newChainedError returns a chainedError made up from the given errors,
// omitting nil values. It returns nil unless at least one of its arguments
// is not nil.
func newChainedError(errs ...error) *chainedError {
	if len(errs) == 0 || (len(errs) == 1 && errs[0] == nil) {
		return nil
	}
	ce := &chainedError{error: errs[0]}
	for _, err := range errs[1:] {
		if err == nil {
			continue
		}
		ce.cause = &chainedError{error: err}
		ce = ce.cause
	}
	return ce
}

// A replicaCorruptionError indicates that the replica has experienced an error
// which puts its integrity at risk.
type replicaCorruptionError struct {
	error
	// processed indicates that the error has been taken into account and
	// necessary steps will be taken. For now, required for testing.
	processed bool
}

// Error implements the error interface.
func (rce *replicaCorruptionError) Error() string {
	if rce == nil {
		rce = newReplicaCorruptionError()
	}
	return fmt.Sprintf("replica corruption (processed=%t): %s", rce.processed, rce.error)
}

// newReplicaCorruptionError creates a new error indicating a corrupt replica,
// with the supplied list of errors given as history.
func newReplicaCorruptionError(err ...error) *replicaCorruptionError {
	return &replicaCorruptionError{error: newChainedError(err...)}
}

// maybeSetCorrupt is a stand-in for proper handling of failing replicas. Such a
// failure is indicated by a call to maybeSetCorrupt with a replicaCorruptionError.
// Currently any error is passed through, but prospectively it should stop the
// range from participating in progress, trigger a rebalance operation and
// decide on an error-by-error basis whether the corruption is limited to the
// range, store, node or cluster with corresponding actions taken.
func (r *Replica) maybeSetCorrupt(err error) error {
	if cErr, ok := err.(*replicaCorruptionError); ok && cErr != nil {
		log.Errorc(r.context(), "stalling replica due to: %s", cErr.error)
		cErr.processed = true
		return cErr
	}
	return err
}

// resolveIntents resolves the given intents. For those which are local to the
// range, we submit directly to the range-local Raft instance; all non-local
// intents are resolved asynchronously in a batch. If `wait` is true, all
// operations are carried out synchronously and an error is returned.
// Otherwise, the call returns without error as soon as all local resolve
// commands have been **proposed** (not executed). This ensures that if a
// waiting client retries immediately after calling this function, it will not
// hit the same intents again.
func (r *Replica) resolveIntents(ctx context.Context, intents []roachpb.Intent, wait bool, poison bool) error {
	trace := tracer.FromCtx(ctx)
	tracer.ToCtx(ctx, nil) // we're doing async stuff below; those need new traces
	trace.Event(fmt.Sprintf("resolving intents [wait=%t]", wait))

	var reqsRemote []roachpb.Request
	baLocal := roachpb.BatchRequest{}
	for i := range intents {
		intent := intents[i] // avoids a race in `i, intent := range ...`
		var resolveArgs roachpb.Request
		var local bool // whether this intent lives on this Range
		{
			header := roachpb.Span{
				Key:    intent.Key,
				EndKey: intent.EndKey,
			}

			if len(intent.EndKey) == 0 {
				resolveArgs = &roachpb.ResolveIntentRequest{
					Span:      header,
					IntentTxn: intent.Txn,
					Poison:    poison,
				}
				local = r.ContainsKey(intent.Key)
			} else {
				resolveArgs = &roachpb.ResolveIntentRangeRequest{
					Span:      header,
					IntentTxn: intent.Txn,
					Poison:    poison,
				}
				local = r.ContainsKeyRange(intent.Key, intent.EndKey)
			}
		}

		// If the intent isn't (completely) local, we'll need to send an external request.
		// We'll batch them all up and send at the end.
		if local {
			baLocal.Add(resolveArgs)
		} else {
			reqsRemote = append(reqsRemote, resolveArgs)
		}
	}

	// The local batch goes directly to Raft.
	var wg sync.WaitGroup
	if len(baLocal.Requests) > 0 {
		action := func() error {
			// Trace this under the ID of the intent owner.
			trace := r.store.Tracer().NewTrace(tracer.Node, baLocal)
			defer trace.Finalize()
			ctx := tracer.ToCtx(ctx, trace)
			_, err := r.addWriteCmd(ctx, baLocal, &wg)
			return err
		}
		wg.Add(1)
		if wait || !r.store.Stopper().RunAsyncTask(func() {
			if err := action(); err != nil {
				log.Warningf("unable to resolve local intents; %s", err)
			}
		}) {
			// Still run the task when draining. Our caller already has a task and
			// going async here again is merely for performance, but some intents
			// need to be resolved because they might block other tasks. See #1684.
			// Note that handleSkippedIntents has a TODO in case #1684 comes back.
			if err := action(); err != nil {
				return err
			}
		}
	}

	// Resolve all of the intents which aren't local to the Range.
	if len(reqsRemote) > 0 {
		b := &client.Batch{}
		b.InternalAddRequest(reqsRemote...)
		action := func() error {
			// TODO(tschottdorf): no tracing here yet.
			return r.store.DB().Run(b)
		}
		if wait || !r.store.Stopper().RunAsyncTask(func() {
			if err := action; err != nil {
				log.Warningf("unable to resolve external intents: %s", err)
			}
		}) {
			// As with local intents, try async to not keep the caller waiting, but
			// when draining just go ahead and do it synchronously. See #1684.
			if err := action(); err != nil {
				return err
			}
		}
	}

	// Wait until the local ResolveIntents batch has been submitted to
	// raft. No-op if all were non-local.
	wg.Wait()
	return nil
}

var errSystemDBIntent = errors.New("must retry later due to intent on SystemDB")

// loadSystemDBSpan scans the entire SystemDB span and returns the full list of
// key/value pairs along with the sha1 checksum of the contents (key and value).
func (r *Replica) loadSystemDBSpan() ([]roachpb.KeyValue, []byte, error) {
	ba := roachpb.BatchRequest{}
	ba.ReadConsistency = roachpb.INCONSISTENT
	ba.Timestamp = r.store.Clock().Now()
	ba.Add(&roachpb.ScanRequest{Span: keys.SystemDBSpan})
	br, intents, err := r.executeBatch(r.store.Engine(), nil, ba)
	if err != nil {
		return nil, nil, err
	}
	if len(intents) > 0 {
		// There were intents, so what we read may not be consistent. Attempt
		// to nudge the intents in case they're expired; next time around we'll
		// hopefully have more luck.
		r.handleSkippedIntents(intents)
		return nil, nil, errSystemDBIntent
	}
	sha := sha1.New()
	kvs := br.Responses[0].GetInner().(*roachpb.ScanResponse).Rows
	for _, kv := range kvs {
		if _, err := sha.Write(kv.Key); err != nil {
			return nil, nil, err
		}
		// There are all kinds of different types here, so we can't use the
		// typed getters.
		if _, err := sha.Write(kv.Value.RawBytes); err != nil {
			return nil, nil, err
		}
	}
	return kvs, sha.Sum(nil), err
}

// maybeAddToSplitQueue checks whether the current size of the range
// exceeds the max size specified in the zone config. If yes, the
// range is added to the split queue.
func (r *Replica) maybeAddToSplitQueue() {
	maxBytes := r.GetMaxBytes()
	if maxBytes > 0 && r.stats.KeyBytes+r.stats.ValBytes > maxBytes {
		r.store.splitQueue.MaybeAdd(r, r.store.Clock().Now())
	}
}


text:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//

//
// common.h - precompiled headers include for the COM+ Execution Engine
//

//
// Make sure _ASSERTE is defined before including this header file
// Other than that, please keep this header self-contained so that it can be included in
//  all dlls
//

#ifndef _stdmacros_h_
#define _stdmacros_h_

#include "specstrings.h"
#include "contract.h"

#ifndef _ASSERTE
#error Please define _ASSERTE before including StdMacros.h
#endif

#ifdef _DEBUG
#define     DEBUG_ARG(x)  , x
#define     DEBUG_ARG1(x)  x
#else
#define     DEBUG_ARG(x)
#define     DEBUG_ARG1(x)
#endif

#ifdef DACCESS_COMPILE
#define     DAC_ARG(x)  , x
#else
#define     DAC_ARG(x)
#endif

/********************************************/
/*         Portability macros               */
/********************************************/

#ifdef _TARGET_AMD64_
#define AMD64_FIRST_ARG(x)  x ,
#define AMD64_ARG(x)        , x
#define AMD64_ONLY(x)       x
#define NOT_AMD64(x)
#define NOT_AMD64_ARG(x)
#else
#define AMD64_FIRST_ARG(x)
#define AMD64_ARG(x)
#define AMD64_ONLY(x)
#define NOT_AMD64(x)        x
#define NOT_AMD64_ARG(x)    , x
#endif

#ifdef _TARGET_X86_
#define X86_FIRST_ARG(x)    x ,
#define X86_ARG(x)          , x
#define X86_ONLY(x)         x
#define NOT_X86(x)
#define NOT_X86_ARG(x)
#else
#define X86_FIRST_ARG(x)
#define X86_ARG(x)
#define X86_ONLY(x)
#define NOT_X86(x)          x
#define NOT_X86_ARG(x)      , x
#endif

#ifdef _WIN64
#define WIN64_ARG(x)  , x
#define WIN64_ONLY(x) x
#define NOT_WIN64(x)
#define NOT_WIN64_ARG(x)
#else
#define WIN64_ARG(x)
#define WIN64_ONLY(x)
#define NOT_WIN64(x)    x
#define NOT_WIN64_ARG(x)    , x
#endif // _WIN64

#ifdef _TARGET_ARM_
#define ARM_FIRST_ARG(x)  x ,
#define ARM_ARG(x)        , x
#define ARM_ONLY(x)       x
#define NOT_ARM(x)
#define NOT_ARM_ARG(x)
#else
#define ARM_FIRST_ARG(x)
#define ARM_ARG(x)
#define ARM_ONLY(x)
#define NOT_ARM(x)        x
#define NOT_ARM_ARG(x)    , x
#endif

#ifdef _TARGET_ARM64_
#define ARM64_FIRST_ARG(x)  x ,
#define ARM64_ARG(x)        , x
#define ARM64_ONLY(x)       x
#define NOT_ARM64(x)
#define NOT_ARM64_ARG(x)
#else
#define ARM64_FIRST_ARG(x)
#define ARM64_ARG(x)
#define ARM64_ONLY(x)
#define NOT_ARM64(x)        x
#define NOT_ARM64_ARG(x)    , x
#endif

#ifdef _WIN64
    #define LOG2_PTRSIZE       3
    #define INVALID_POINTER_CC 0xcccccccccccccccc
    #define INVALID_POINTER_CD 0xcdcdcdcdcdcdcdcd
    #define FMT_ADDR           " %08x`%08x "
    #define LFMT_ADDR          W(" %08x`%08x ")
    #define DBG_ADDR(ptr)      (((UINT_PTR) (ptr)) >> 32), (((UINT_PTR) (ptr)) & 0xffffffff)
#else // _WIN64
    #define LOG2_PTRSIZE       2
    #define INVALID_POINTER_CC 0xcccccccc
    #define INVALID_POINTER_CD 0xcdcdcdcd
    #define FMT_ADDR           " %08x "
    #define LFMT_ADDR          W(" %08x ")
    #define DBG_ADDR(ptr)      ((UINT_PTR)(ptr))
#endif // _WIN64

#ifdef _TARGET_ARM_
    #define ALIGN_ACCESS        ((1<<LOG2_PTRSIZE)-1)
#endif

#ifndef ALLOC_ALIGN_CONSTANT
#define ALLOC_ALIGN_CONSTANT ((1<<LOG2_PTRSIZE)-1)
#endif

inline void *GetTopMemoryAddress(void)
{
    WRAPPER_NO_CONTRACT;

    static void *result; // = NULL;
    if( NULL == result )
    {
        SYSTEM_INFO sysInfo;
        GetSystemInfo( &sysInfo );
        result = sysInfo.lpMaximumApplicationAddress;
    }
    return result;
}
inline void *GetBotMemoryAddress(void)
{
    WRAPPER_NO_CONTRACT;

    static void *result; // = NULL;
    if( NULL == result )
    {
        SYSTEM_INFO sysInfo;
        GetSystemInfo( &sysInfo );
        result = sysInfo.lpMinimumApplicationAddress;
    }
    return result;
}

#define TOP_MEMORY (GetTopMemoryAddress())
#define BOT_MEMORY (GetBotMemoryAddress())

//
// This macro returns val rounded up as necessary to be a multiple of alignment; alignment must be a power of 2
//
inline size_t ALIGN_UP( size_t val, size_t alignment )
{
    LIMITED_METHOD_DAC_CONTRACT;

    // alignment must be a power of 2 for this implementation to work (need modulo otherwise)
    _ASSERTE( 0 == (alignment & (alignment - 1)) );
    size_t result = (val + (alignment - 1)) & ~(alignment - 1);
    _ASSERTE( result >= val );      // check for overflow
    return result;
}
inline void* ALIGN_UP( void* val, size_t alignment )
{
    WRAPPER_NO_CONTRACT;

    return (void*) ALIGN_UP( (size_t)val, alignment );
}
inline uint8_t* ALIGN_UP( uint8_t* val, size_t alignment )
{
    WRAPPER_NO_CONTRACT;

    return (uint8_t*) ALIGN_UP( (size_t)val, alignment );
}

inline size_t ALIGN_DOWN( size_t val, size_t alignment )
{
    LIMITED_METHOD_CONTRACT;

    // alignment must be a power of 2 for this implementation to work (need modulo otherwise)
    _ASSERTE( 0 == (alignment & (alignment - 1)) );
    size_t result = val & ~(alignment - 1);
    return result;
}
inline void* ALIGN_DOWN( void* val, size_t alignment )
{
    WRAPPER_NO_CONTRACT;
    return (void*) ALIGN_DOWN( (size_t)val, alignment );
}
inline uint8_t* ALIGN_DOWN( uint8_t* val, size_t alignment )
{
    WRAPPER_NO_CONTRACT;
    return (uint8_t*) ALIGN_DOWN( (size_t)val, alignment );
}

inline BOOL IS_ALIGNED( size_t val, size_t alignment )
{
    LIMITED_METHOD_CONTRACT;
    SUPPORTS_DAC;

    // alignment must be a power of 2 for this implementation to work (need modulo otherwise)
    _ASSERTE( 0 == (alignment & (alignment - 1)) );
    return 0 == (val & (alignment - 1));
}
inline BOOL IS_ALIGNED( const void* val, size_t alignment )
{
    WRAPPER_NO_CONTRACT;
    return IS_ALIGNED( (size_t) val, alignment );
}

// Rounds a ULONG up to the nearest power of two number.
inline ULONG RoundUpToPower2(ULONG x)
{
    if (x == 0) return 1;

    x = x - 1;
    x = x | (x >> 1);
    x = x | (x >> 2);
    x = x | (x >> 4);
    x = x | (x >> 8);
    x = x | (x >> 16);
    return x + 1;
}

#ifdef ALIGN_ACCESS

// NOTE: pSrc is evaluated three times!!!
#define MAYBE_UNALIGNED_READ(pSrc, bits)        (IS_ALIGNED((size_t)(pSrc), sizeof(UINT##bits)) ? \
                                                    (*(UINT##bits*)      (pSrc)) : \
                                                    (GET_UNALIGNED_##bits(pSrc)) )

#define MAYBE_UNALIGNED_WRITE(pDst, bits, expr) do { if (IS_ALIGNED((size_t)(pDst), sizeof(UINT##bits))) \
                                                    *(UINT##bits*)(pDst) = (UINT##bits)(expr); else \
                                                    SET_UNALIGNED_##bits(pDst, (UINT##bits)(expr)); } while (0)

// these are necessary for MAYBE_UNALIGNED_XXX to work with UINT_PTR
#define GET_UNALIGNED__PTR(x) GET_UNALIGNED_PTR(x)
#define SET_UNALIGNED__PTR(p,x) SET_UNALIGNED_PTR(p,x)

#else // ALIGN_ACCESS
#define MAYBE_UNALIGNED_READ(pSrc, bits)        (*(UINT##bits*)(pSrc))
#define MAYBE_UNALIGNED_WRITE(pDst, bits, expr) do { *(UINT##bits*)(pDst) = (UINT##bits)(expr); } while(0)
#endif // ALIGN_ACCESS

//
// define some useful macros for logging object
//

#define FMT_OBJECT  "object" FMT_ADDR
#define FMT_HANDLE  "handle" FMT_ADDR
#define FMT_CLASS   "%s"
#define FMT_REG     "r%d "
#define FMT_STK     "sp%s0x%02x "
#define FMT_PIPTR   "%s%s pointer "

#define DBG_GET_CLASS_NAME(pMT)        \
        (pMT)->GetClass()->GetDebugClassName()

#define DBG_CLASS_NAME_MT(pMT)         \
        (DBG_GET_CLASS_NAME(pMT) == NULL) ? "<null-class>" : DBG_GET_CLASS_NAME(pMT)

#define DBG_GET_MT_FROM_OBJ(obj)       \
        (MethodTable*)((size_t)((Object*) (obj))->GetGCSafeMethodTable())

#define DBG_CLASS_NAME_OBJ(obj)        \
        ((obj) == NULL)  ? "null" : DBG_CLASS_NAME_MT(DBG_GET_MT_FROM_OBJ(obj))

#define DBG_CLASS_NAME_IPTR2(obj,iptr) \
        ((iptr) != 0)    ? ""     : DBG_CLASS_NAME_MT(DBG_GET_MT_FROM_OBJ(obj))

#define DBG_CLASS_NAME_IPTR(obj,iptr)  \
        ((obj)  == NULL) ? "null" : DBG_CLASS_NAME_IPTR2(obj,iptr)

#define DBG_STK(off)                   \
        (off >= 0) ? "+" : "-",        \
        (off >= 0) ? off : -off

#define DBG_PIN_NAME(pin)              \
        (pin)  ? "pinned "  : ""

#define DBG_IPTR_NAME(iptr)            \
        (iptr) ? "interior" : "base"

#define LOG_HANDLE_OBJECT_CLASS(str1, hnd, str2, obj)    \
        str1 FMT_HANDLE str2 FMT_OBJECT FMT_CLASS "\n",  \
        DBG_ADDR(hnd), DBG_ADDR(obj), DBG_CLASS_NAME_OBJ(obj)

#define LOG_OBJECT_CLASS(obj)                            \
        FMT_OBJECT FMT_CLASS "\n",                       \
        DBG_ADDR(obj), DBG_CLASS_NAME_OBJ(obj)

#define LOG_PIPTR_OBJECT_CLASS(obj, pin, iptr)           \
        FMT_PIPTR FMT_ADDR FMT_CLASS "\n",               \
        DBG_PIN_NAME(pin), DBG_IPTR_NAME(iptr),          \
        DBG_ADDR(obj), DBG_CLASS_NAME_IPTR(obj,iptr)

#define LOG_HANDLE_OBJECT(str1, hnd, str2, obj)          \
        str1 FMT_HANDLE str2 FMT_OBJECT "\n",            \
        DBG_ADDR(hnd), DBG_ADDR(obj)

#define LOG_PIPTR_OBJECT(obj, pin, iptr)                 \
        FMT_PIPTR FMT_ADDR "\n",                         \
        DBG_PIN_NAME(pin), DBG_IPTR_NAME(iptr),          \
        DBG_ADDR(obj)

#define UNIQUE_LABEL_DEF(a,x)           a##x
#define UNIQUE_LABEL_DEF_X(a,x)         UNIQUE_LABEL_DEF(a,x)
#ifdef _MSC_VER
#define UNIQUE_LABEL(a)                 UNIQUE_LABEL_DEF_X(_unique_label_##a##_, __COUNTER__)
#else
#define UNIQUE_LABEL(a)                 UNIQUE_LABEL_DEF_X(_unique_label_##a##_, __LINE__)
#endif

#ifndef _countof
#define _countof(_array) (sizeof(_array)/sizeof(_array[0]))
#endif

// This is temporary.  LKG should provide these macros and we should then
// remove STRUNCATE and _TRUNCATE from here.

/* error codes */
#if !defined(STRUNCATE)
#define STRUNCATE       80
#endif

/* _TRUNCATE */
#if !defined(_TRUNCATE)
#define _TRUNCATE ((size_t)-1)
#endif

#endif //_stdmacros_h_


text:
title: 'Hitman Contracts playthrough: Mission 3'
date: '2007-12-13T08:00:00+00:00'
lastmod: '2013-12-09T12:20:54+00:00'
created: '2013-12-09T12:20:54+00:00'
slug: hitman-contracts-playthrough-mission-3
draft: false
tags:
- '47'
- 'contracts'
- 'fuchs'
- 'hitman'
- 'nuclear'
- 'playthrough'
- 'runthrough'
- 'siberia'
- 'silent assassin'
- 'the bjarkhov bomb'
- 'tundra'
- 'vodka'
- 'zero bullet'
categories:
- 'Hitman: Contracts'
- 'Videogames'
original_id: 399e1be2-49c1-4a91-be87-cf89921d8d7f
---

Mission three: The Bjarkhov Bomb. A Siberian tundra holds a beached nuclear submarine with a dangerous bomb making facility within; Fabian Fuchs and rebel Commander Bjarkhov intend to strike a deal which could spell twenty kilotonnes of trouble.

Hitman Contracts contains mature content and is wholly unsuitable for minors, some of these videos also contain flashing lights; fairly warned be thee.

Download the higher quality version (130MB, XviD video, VBR MP3 audio)

Notes

I will readily admit that I was slightly squiffy while doing this commentary which is why there are so many niggling errors and I almost trip over my own words more than once. My terminal malfunction of referring to disguises as "uniforms" returns in force while a recurring problem of referring to my challenge as a "no zero" run rather than an "all zero" run; this is just a partial shift from my regularly mentioned "no bullet" run. This is the one mission where I yearned for a more advanced (commercial) sequential video editor than VirtualDub which would perhaps allow me to speed up the dead-zones in this mission taken up with traveling. There are some audio abnormalities in several places where the audio for a cut-scene stops after the animation has stopped; as far as I know there isn't any audio corruption going on.

I tend to ramble a bit when trying to describe the submarine section and how to go about taking out the patrolling guard; it isn't so confusing in context but I seem to remember gesticulating when identifying guards which probably doesn't translate well to sound.

Details

Play time: 14:51

Rating: Silent Assassin

Difficulty: Professional

Shots fired: 0

Alerts: 0

While certainly not one of the most complex missions in Contracts, The Bjarkhov Bomb is certainly one of the most expansive. The distance between each objective is the big time sink and a lot of the time is spent running between points rather than doing anything noteworthy. Fuchs' disguise is the one that will get you unrestricted access to areas while the civilian uniform will allow you to run around the initial area unhindered, although obviously doing that is just asking for trouble. There are other possibilities of killing Fuch's including sniping him when he (eventually) goes for the train, there is a sniper rifle in the tower above the service area for this very purpose, obviously this draws attention and is rarely worth the wait.

Bjarkhov isn't very taxing as long as you're in Fuchs' disguise otherwise it would likely be a slow and tedious approach using a guard disguise and probably a few bullets. The submarine section is relatively easy and finding the bomb points is the most taxing part; in a radiation suit you have all the benefits of the civilian disguise in being able to run everywhere without risking a cover break. If you didn't feel like picking up the bombs from Bjarkhov's ship then the bombs in the submarine are towards the rear of it, as soon as you go down the stairs head right and you should find them in a small room off to the back.

The escape can either be on foot as I did, or on the train. The train doesn't require a guard to be on it as well so if you choose that route then just start it rather than hanging around.


text:
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                Toggle navigation

            </button>
            INCA

                    Vehículos

Agregar

Listados

                    E. Cómputo

Agregar

Listados

                    Mobiliario

Agregar

Listados

Inca México

                    <?php echo strtoupper($_SESSION['nombre']); ?>

<a href="<?php base_url() ?>index.php/dashboard/perfil">Perfil

Resguardo

Notificaciones

Cerrar Sesión