From 9670f669808a704a502324906c986f33f44c7c04 Mon Sep 17 00:00:00 2001 From: Dennis Balzuweit <52537643+dbcto@users.noreply.github.com> Date: Mon, 27 Sep 2021 21:40:02 +0200 Subject: [PATCH 1/4] CHG: Unnecessary variables removed --- Grijjy.MongoDB.pas | 3 --- 1 file changed, 3 deletions(-) diff --git a/Grijjy.MongoDB.pas b/Grijjy.MongoDB.pas index 8dbf29d..8b2a09b 100644 --- a/Grijjy.MongoDB.pas +++ b/Grijjy.MongoDB.pas @@ -1200,8 +1200,6 @@ function TgoMongoClient.GetInstanceInfo(const ASaslSupportedMechs: String = ''; Reply: IgoMongoReply; Doc: TgoBsonDocument; InstArray: TgoBsonArray; - Databases: TgoBsonArray; - Value: TgoBsonValue; I: Integer; begin Writer := TgoBsonWriter.Create; @@ -1335,7 +1333,6 @@ function TgoMongoDatabase.CreateCollection(const AName: String; var Writer: IgoBsonWriter; Reply: IgoMongoReply; - i: Integer; begin Writer := TgoBsonWriter.Create; From 54e651f96b236c1ea1f2e458a86134fce04414ce Mon Sep 17 00:00:00 2001 From: Michael Riedel Date: Tue, 23 Nov 2021 11:41:27 +0100 Subject: [PATCH 2/4] CHG: Added default parameter ForceWaitTimeout to OpQuery and WaitForReply Call of GetInstanceInfo() results in an endless loop in WaitForReply when replicasets may have changed primary and secondary, because there is never any data received even if replicaset got valid again --- Grijjy.MongoDB.Protocol.pas | 20 +++++++++++++++----- Grijjy.MongoDB.pas | 4 ++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Grijjy.MongoDB.Protocol.pas b/Grijjy.MongoDB.Protocol.pas index 30d7cea..1c1a61a 100644 --- a/Grijjy.MongoDB.Protocol.pas +++ b/Grijjy.MongoDB.Protocol.pas @@ -185,7 +185,7 @@ TgoMongoProtocol = class FAuthErrorCode: Integer; private procedure Send(const AData: TBytes); - function WaitForReply(const ARequestId: Integer): IgoMongoReply; + function WaitForReply(const ARequestId: Integer; forceWaitTimeout : Boolean = false): IgoMongoReply; function TryGetReply(const ARequestId: Integer; out AReply: IgoMongoReply): Boolean; inline; function LastPartialReply(const ARequestID: Integer; out ALastRecv: TDateTime): Boolean; function OpReplyValid(out AIndex: Integer): Boolean; @@ -254,7 +254,8 @@ TgoMongoProtocol = class function OpQuery(const AFullCollectionName: UTF8String; const AFlags: TgoMongoQueryFlags; const ANumberToSkip, ANumberToReturn: Integer; const AQuery: TBytes; - const AReturnFieldsSelector: TBytes = nil): IgoMongoReply; + const AReturnFieldsSelector: TBytes = nil; + const forceWaitTimeout : Boolean = false): IgoMongoReply; { Implements the OP_GET_MORE opcode, used to get an additional page of documents from the database. @@ -700,7 +701,8 @@ function TgoMongoProtocol.OpGetMore(const AFullCollectionName: UTF8String; function TgoMongoProtocol.OpQuery(const AFullCollectionName: UTF8String; const AFlags: TgoMongoQueryFlags; const ANumberToSkip, ANumberToReturn: Integer; const AQuery, - AReturnFieldsSelector: TBytes): IgoMongoReply; + AReturnFieldsSelector: TBytes; + const forceWaitTimeout : Boolean): IgoMongoReply; { https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#wire-op-query } var Header: TMsgHeader; @@ -735,7 +737,7 @@ function TgoMongoProtocol.OpQuery(const AFullCollectionName: UTF8String; finally Data.Free; end; - Result := WaitForReply(Header.RequestID); + Result := WaitForReply(Header.RequestID, forceWaitTimeout); end; function TgoMongoProtocol.OpReplyMsgHeader(out AMsgHeader): Boolean; @@ -885,11 +887,13 @@ function TgoMongoProtocol.TryGetReply(const ARequestId: Integer; end; function TgoMongoProtocol.WaitForReply( - const ARequestId: Integer): IgoMongoReply; + const ARequestId: Integer; forceWaitTimeout : Boolean = false): IgoMongoReply; var LastRecv: TDateTime; + InitRecv: TDateTime; begin Result := nil; + InitRecv := Now; while (ConnectionState = TgoConnectionState.Connected) and (not TryGetReply(ARequestID, Result)) do begin @@ -897,6 +901,12 @@ function TgoMongoProtocol.WaitForReply( (MillisecondsBetween(Now, LastRecv) > FSettings.ReplyTimeout) then Break; + // in case we didn't receive any response, stop if timout + // is reached and forceWaitTimeout=true is passed + if ((Int(LastRecv) = 0) and (forceWaitTimeout = true) and + (MillisecondsBetween(Now, InitRecv) > FSettings.ReplyTimeout)) + then + Break; Sleep(5); end; diff --git a/Grijjy.MongoDB.pas b/Grijjy.MongoDB.pas index 8b2a09b..ad66f77 100644 --- a/Grijjy.MongoDB.pas +++ b/Grijjy.MongoDB.pas @@ -1204,7 +1204,7 @@ function TgoMongoClient.GetInstanceInfo(const ASaslSupportedMechs: String = ''; begin Writer := TgoBsonWriter.Create; Writer.WriteStartDocument; - Writer.WriteInt32('isMaster', 1); + Writer.WriteInt32('hello', 1); if (Length(ASaslSupportedMechs) > 0) then begin Writer.WriteString('saslSupportedMechs', ASaslSupportedMechs); @@ -1212,7 +1212,7 @@ function TgoMongoClient.GetInstanceInfo(const ASaslSupportedMechs: String = ''; Writer.WriteString('Comment', AComment); end; Writer.WriteEndDocument; - Reply := FProtocol.OpQuery(COLLECTION_ADMIN_COMMAND, [], 0, -1, Writer.ToBson, nil); + Reply := FProtocol.OpQuery(COLLECTION_ADMIN_COMMAND, [], 0, -1, Writer.ToBson, nil, true); HandleCommandReply(Reply); if not(Reply.Documents = nil) then From 78208e090260e930b20a481b09deab7153579712 Mon Sep 17 00:00:00 2001 From: Dennis Balzuweit <52537643+dbcto@users.noreply.github.com> Date: Thu, 25 Nov 2021 16:04:49 +0100 Subject: [PATCH 3/4] CHG: Var forceWaitTimeout to AForceWaitTimeout --- Grijjy.MongoDB.Protocol.pas | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Grijjy.MongoDB.Protocol.pas b/Grijjy.MongoDB.Protocol.pas index ab068c5..605578c 100644 --- a/Grijjy.MongoDB.Protocol.pas +++ b/Grijjy.MongoDB.Protocol.pas @@ -186,7 +186,7 @@ TgoMongoProtocol = class FAuthErrorCode: Integer; private procedure Send(const AData: TBytes); - function WaitForReply(const ARequestId: Integer; forceWaitTimeout : Boolean = false): IgoMongoReply; + function WaitForReply(const ARequestId: Integer; AForceWaitTimeout : Boolean = false): IgoMongoReply; function TryGetReply(const ARequestId: Integer; out AReply: IgoMongoReply): Boolean; inline; function LastPartialReply(const ARequestID: Integer; out ALastRecv: TDateTime): Boolean; function OpReplyValid(out AIndex: Integer): Boolean; @@ -256,7 +256,7 @@ TgoMongoProtocol = class const AFlags: TgoMongoQueryFlags; const ANumberToSkip, ANumberToReturn: Integer; const AQuery: TBytes; const AReturnFieldsSelector: TBytes = nil; - const forceWaitTimeout : Boolean = false): IgoMongoReply; + const AForceWaitTimeout : Boolean = false): IgoMongoReply; { Implements the OP_GET_MORE opcode, used to get an additional page of documents from the database. @@ -752,7 +752,7 @@ function TgoMongoProtocol.OpQuery(const AFullCollectionName: UTF8String; const AFlags: TgoMongoQueryFlags; const ANumberToSkip, ANumberToReturn: Integer; const AQuery, AReturnFieldsSelector: TBytes; - const forceWaitTimeout : Boolean): IgoMongoReply; + const AForceWaitTimeout : Boolean): IgoMongoReply; { https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#wire-op-query } var Header: TMsgHeader; @@ -787,7 +787,7 @@ function TgoMongoProtocol.OpQuery(const AFullCollectionName: UTF8String; finally Data.Free; end; - Result := WaitForReply(Header.RequestID, forceWaitTimeout); + Result := WaitForReply(Header.RequestID, AForceWaitTimeout); end; function TgoMongoProtocol.OpReplyMsgHeader(out AMsgHeader): Boolean; @@ -937,7 +937,7 @@ function TgoMongoProtocol.TryGetReply(const ARequestId: Integer; end; function TgoMongoProtocol.WaitForReply( - const ARequestId: Integer; forceWaitTimeout : Boolean = false): IgoMongoReply; + const ARequestId: Integer; AForceWaitTimeout : Boolean = false): IgoMongoReply; var LastRecv: TDateTime; InitRecv: TDateTime; @@ -952,8 +952,8 @@ function TgoMongoProtocol.WaitForReply( then Break; // in case we didn't receive any response, stop if timout - // is reached and forceWaitTimeout=true is passed - if ((Int(LastRecv) = 0) and (forceWaitTimeout = true) and + // is reached and AForceWaitTimeout=true is passed + if ((Int(LastRecv) = 0) and (AForceWaitTimeout = true) and (MillisecondsBetween(Now, InitRecv) > FSettings.ReplyTimeout)) then Break; From c81da66c2b1dd0620fa28db59cf52701e2893491 Mon Sep 17 00:00:00 2001 From: Dennis Balzuweit <52537643+dbcto@users.noreply.github.com> Date: Thu, 25 Nov 2021 16:15:04 +0100 Subject: [PATCH 4/4] CHG: hello changed to isMaster --- Grijjy.MongoDB.pas | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Grijjy.MongoDB.pas b/Grijjy.MongoDB.pas index 7b0df5d..5bb7fde 100644 --- a/Grijjy.MongoDB.pas +++ b/Grijjy.MongoDB.pas @@ -1205,7 +1205,7 @@ function TgoMongoClient.GetInstanceInfo(const ASaslSupportedMechs: String = ''; begin Writer := TgoBsonWriter.Create; Writer.WriteStartDocument; - Writer.WriteInt32('hello', 1); + Writer.WriteInt32('isMaster', 1); if (Length(ASaslSupportedMechs) > 0) then begin Writer.WriteString('saslSupportedMechs', ASaslSupportedMechs);