From c1fe0cb8e9aeb39c9964dda99f8ec9201e095fc2 Mon Sep 17 00:00:00 2001 From: Volodymyr Mostepaniuk Date: Tue, 9 Feb 2021 19:29:55 +0200 Subject: [PATCH 1/3] Put sequence id from last changes request. --- src/Replication.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Replication.php b/src/Replication.php index d2a7272..2a739c6 100644 --- a/src/Replication.php +++ b/src/Replication.php @@ -192,18 +192,17 @@ public function getReplicationLog() */ public function putReplicationLog(array $response) { $sessionId = \md5((\microtime(true) * 1000000)); - $sourceInfo = $this->source->getDatabaseInfo($this->source->getDatabase()); $data = [ '_id' => '_local/' . $this->task->getRepId(), 'history' => [ - 'recorded_seq' => $sourceInfo['update_seq'], + 'recorded_seq' => $response['end_last_seq'], 'session_id' => $sessionId, 'start_time' => $this->startTime->format('D, d M Y H:i:s e'), 'end_time' => $this->endTime->format('D, d M Y H:i:s e'), ], 'replication_id_version' => 3, 'session_id' => $sessionId, - 'source_last_seq' => $sourceInfo['update_seq'] + 'source_last_seq' => $response['end_last_seq'], ]; if (isset($response['doc_write_failures'])) { @@ -473,9 +472,7 @@ public function locateChangedDocumentsAndReplicate($printStatus, $getFinalReport if (isset($changes['results'][0]['seq'])) { $finalResponse['start_last_seq'] = $changes['results'][0]['seq']; } - if (isset($changes['last_seq'])) { - $finalResponse['end_last_seq'] = $changes['last_seq']; - } + $finalResponse['end_last_seq'] = $since; foreach ($response['multipartResponse'] as $docID => $res) { // Add the response of posting each revision of the // doc that had attachments. From 9d9f0fafffe4a5ec2e345a7f33ff0669107d0ee4 Mon Sep 17 00:00:00 2001 From: Volodymyr Mostepaniuk Date: Tue, 16 Feb 2021 15:54:12 +0200 Subject: [PATCH 2/3] Return previous code as fallback. --- src/Replication.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Replication.php b/src/Replication.php index 2a739c6..c26fef0 100644 --- a/src/Replication.php +++ b/src/Replication.php @@ -192,17 +192,26 @@ public function getReplicationLog() */ public function putReplicationLog(array $response) { $sessionId = \md5((\microtime(true) * 1000000)); + if (!empty($response['end_last_seq'])) { + $last_sequence_id = $response['end_last_seq']; + } + else { + // For some reason end_last_seq was 0 on production. Use previous + // code as fallback value. + $sourceInfo = $this->source->getDatabaseInfo($this->source->getDatabase()); + $last_sequence_id = $sourceInfo['update_seq']; + } $data = [ '_id' => '_local/' . $this->task->getRepId(), 'history' => [ - 'recorded_seq' => $response['end_last_seq'], + 'recorded_seq' => $last_sequence_id, 'session_id' => $sessionId, 'start_time' => $this->startTime->format('D, d M Y H:i:s e'), 'end_time' => $this->endTime->format('D, d M Y H:i:s e'), ], 'replication_id_version' => 3, 'session_id' => $sessionId, - 'source_last_seq' => $response['end_last_seq'], + 'source_last_seq' => $last_sequence_id, ]; if (isset($response['doc_write_failures'])) { From 3d4b66fc909010655938ab76fbe99096e7018093 Mon Sep 17 00:00:00 2001 From: Volodymyr Mostepaniuk Date: Tue, 16 Feb 2021 16:56:56 +0200 Subject: [PATCH 3/3] Subtract 5 minutes to prevent race condition issue. --- src/Replication.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Replication.php b/src/Replication.php index c26fef0..647ed4f 100644 --- a/src/Replication.php +++ b/src/Replication.php @@ -196,10 +196,11 @@ public function putReplicationLog(array $response) { $last_sequence_id = $response['end_last_seq']; } else { - // For some reason end_last_seq was 0 on production. Use previous - // code as fallback value. + // For some reason end_last_seq was 0 on production for some cases. + // Use previous code as fallback value just subtract 5 minutes to + // prevent race condition issue. $sourceInfo = $this->source->getDatabaseInfo($this->source->getDatabase()); - $last_sequence_id = $sourceInfo['update_seq']; + $last_sequence_id = $sourceInfo['update_seq'] - 5 * 60 * 1000000; } $data = [ '_id' => '_local/' . $this->task->getRepId(),