Skip to content

Commit c725bee

Browse files
committed
CBG-4413: Add Sync Function Logging capture to Diagnostic API
- Also fixes regression in SyncRunner pool use from Diagnostic API - Refactor tests for clearer test cases and improved reliability
1 parent 1060af7 commit c725bee

File tree

3 files changed

+205
-221
lines changed

3 files changed

+205
-221
lines changed

db/crud.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,7 +1698,6 @@ func (db *DatabaseCollectionWithUser) PutExistingRevWithBody(ctx context.Context
16981698
// access map for users, roles, handler errors and sync fn exceptions.
16991699
// If syncFn is provided, it will be used instead of the one configured on the database.
17001700
func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, newDoc, oldDoc *Document, syncFn string, errorLogFunc, infoLogFunc func(string)) (*channels.ChannelMapperOutput, error) {
1701-
17021701
mutableBody, metaMap, _, err := db.prepareSyncFn(oldDoc, newDoc)
17031702
if err != nil {
17041703
base.InfofCtx(ctx, base.KeyDiagnostic, "Failed to prepare to run sync function: %v", err)
@@ -1709,32 +1708,30 @@ func (db *DatabaseCollectionWithUser) SyncFnDryrun(ctx context.Context, newDoc,
17091708
if err != nil {
17101709
return nil, err
17111710
}
1712-
var output *channels.ChannelMapperOutput
1713-
var syncErr error
1714-
if syncFn == "" && db.ChannelMapper != nil {
1715-
output, err = db.ChannelMapper.MapToChannelsAndAccess(ctx, mutableBody, string(oldDoc._rawBody), metaMap, syncOptions)
1716-
if err != nil {
1717-
return nil, &base.SyncFnDryRunError{Err: err}
1718-
}
1719-
} else {
1720-
if syncFn == "" {
1711+
1712+
// fetch configured sync function if one is not provided
1713+
if syncFn == "" {
1714+
if db.ChannelMapper != nil {
1715+
syncFn = db.ChannelMapper.Function()
1716+
} else {
17211717
scopeAndCollectionName := db.ScopeAndCollectionName()
17221718
syncFn = channels.GetDefaultSyncFunction(scopeAndCollectionName.Scope, scopeAndCollectionName.Collection)
17231719
}
1724-
jsTimeout := time.Duration(base.DefaultJavascriptTimeoutSecs) * time.Second
1725-
syncRunner, err := channels.NewSyncRunnerWithLogging(ctx, syncFn, jsTimeout, errorLogFunc, infoLogFunc)
1726-
if err != nil {
1727-
return nil, fmt.Errorf("failed to create sync runner: %v", err)
1728-
}
1729-
jsOutput, err := syncRunner.Call(ctx, mutableBody, string(oldDoc._rawBody), metaMap, syncOptions)
1730-
if err != nil {
1720+
}
17311721

1732-
return nil, &base.SyncFnDryRunError{Err: err}
1733-
}
1734-
output = jsOutput.(*channels.ChannelMapperOutput)
1722+
// create new sync runner instance for this dry run
1723+
jsTimeout := time.Duration(base.DefaultJavascriptTimeoutSecs) * time.Second
1724+
syncRunner, err := channels.NewSyncRunnerWithLogging(ctx, syncFn, jsTimeout, errorLogFunc, infoLogFunc)
1725+
if err != nil {
1726+
return nil, fmt.Errorf("failed to create sync runner: %v", err)
1727+
}
1728+
1729+
jsOutput, err := syncRunner.Call(ctx, mutableBody, sgbucket.JSONString(oldDoc._rawBody), metaMap, syncOptions)
1730+
if err != nil {
1731+
return nil, &base.SyncFnDryRunError{Err: err}
17351732
}
17361733

1737-
return output, syncErr
1734+
return jsOutput.(*channels.ChannelMapperOutput), nil
17381735
}
17391736

17401737
// revTreeConflictCheck checks for conflicts in the rev tree history and returns the parent revid, currentRevIndex

rest/diagnostic_doc_api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type SyncFnDryRun struct {
3030
Channels base.Set `json:"channels"`
3131
Access channels.AccessMap `json:"access"`
3232
Roles channels.AccessMap `json:"roles"`
33-
Exception string `json:"exception"`
33+
Exception string `json:"exception,omitempty"`
3434
Expiry *uint32 `json:"expiry,omitempty"`
3535
Logging SyncFnDryRunLogging `json:"logging"`
3636
}
@@ -89,7 +89,7 @@ func (h *handler) handleSyncFnDryRun() error {
8989
}
9090

9191
if syncDryRunPayload.Doc == nil && docid == "" {
92-
return base.HTTPErrorf(http.StatusBadRequest, "no docid or document provided")
92+
return base.HTTPErrorf(http.StatusBadRequest, "no doc_id or document provided")
9393
}
9494

9595
oldDoc := &db.Document{ID: docid}

0 commit comments

Comments
 (0)