Skip to content

Commit 067d120

Browse files
chore: add logging to help investigate hang - WPB-22986 πŸ’ (#4281)
Co-authored-by: Sam Wyndham <samwyndham@users.noreply.github.com>
1 parent 5502e37 commit 067d120

File tree

5 files changed

+55
-20
lines changed

5 files changed

+55
-20
lines changed

β€ŽWireLogging/Sources/WireLogging/LogAttributes.swiftβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public enum LogAttributesKey: String, Comparable, Sendable {
4848
case syncType = "sync_type"
4949
case syncVersion = "sync_version"
5050
case workItemID = "work_item_id"
51+
case isNewClient = "is_new_client"
5152

5253
public static func < (lhs: LogAttributesKey, rhs: LogAttributesKey) -> Bool {
5354
lhs.rawValue < rhs.rawValue

β€ŽWireLogging/Sources/WireLogging/WireLogger+MessageTime.swiftβ€Ž

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,47 @@ public extension WireLogger {
2525
attributes: LogAttributes = [:],
2626
block: () async throws -> T
2727
) async throws -> T {
28+
let context = measureTimeStart(label: label, attributes: attributes)
29+
let result = try await block()
30+
measureTimeEnd(context: context)
31+
return result
32+
}
33+
34+
func measureTime<T, E: Error>(
35+
label: String,
36+
attributes: LogAttributes = [:],
37+
block: () throws(E) -> T
38+
) throws(E) -> T {
39+
let context = measureTimeStart(label: label, attributes: attributes)
40+
let result = try block()
41+
measureTimeEnd(context: context)
42+
return result
43+
}
44+
45+
// MARK: - Helpers
46+
47+
private struct Context {
48+
let start: Date
49+
let label: String
50+
let attributes: LogAttributes
51+
}
52+
53+
private func measureTimeStart(
54+
label: String,
55+
attributes: LogAttributes
56+
) -> Context {
2857
let startMessage = "starting \(label)"
2958
info(startMessage, attributes: attributes)
30-
let start = Date.now
31-
let result = try await block()
32-
let durationInSeconds = start.timeIntervalSinceNow.magnitude
33-
var updatedAttributes = attributes
59+
return Context(start: Date.now, label: label, attributes: attributes)
60+
}
61+
62+
private func measureTimeEnd(context: Context) {
63+
let durationInSeconds = context.start.timeIntervalSinceNow.magnitude
64+
var updatedAttributes = context.attributes
3465
let formattedDuration = String(format: "%.2f", durationInSeconds)
3566
updatedAttributes[.duration] = formattedDuration
36-
let completedMessage = "completed \(label)"
67+
let completedMessage = "completed \(context.label)"
3768
info(completedMessage, attributes: updatedAttributes)
38-
return result
3969
}
4070

4171
}

β€Žwire-ios-sync-engine/Source/Synchronization/StrategyDirectory.swiftβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public class StrategyDirectory: NSObject, StrategyDirectoryProtocol {
310310
]
311311
}
312312

313-
func makeClientRelatedStategies(
313+
func makeClientRelatedStrategies(
314314
applicationStatusDirectory: ApplicationStatusDirectory,
315315
syncContext: NSManagedObjectContext,
316316
transportSession: TransportSessionType,

β€Žwire-ios-sync-engine/Source/UserSession/ZMUserSession/ZMUserSession.swiftβ€Ž

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,12 @@ public final class ZMUserSession: NSObject {
607607

608608
// Create and perform sync if there is a self client.
609609
if let selfClientID = selfUserClient.remoteIdentifier {
610-
setUpSyncAgent(clientID: selfClientID)
610+
setUpSyncAgent(clientID: selfClientID, isNewClient: false)
611611
}
612612
}
613613
}
614614

615-
func setUpSyncAgent(clientID: String) {
615+
func setUpSyncAgent(clientID: String, isNewClient: Bool) {
616616
let clientSessionComponent = userSessionComponent.clientSessionComponent(
617617
clientID: clientID,
618618
completionHandlers: .init(
@@ -654,15 +654,19 @@ public final class ZMUserSession: NSObject {
654654
syncAgent: syncAgent,
655655
notificationContext: notificationContext
656656
)
657-
strategyDirectory.makeClientRelatedStategies(
658-
applicationStatusDirectory: applicationStatusDirectory,
659-
syncContext: syncContext,
660-
transportSession: transportSession,
661-
pushMessageHandler: localNotificationDispatcher,
662-
flowManager: flowManager,
663-
incrementalSyncObserver: incrementalSyncObserver,
664-
metadata: resolvedBackendMetadata
665-
)
657+
658+
// TODO: [WPB-22986] Remove logging - added this logging temporarily to investigate a hang.
659+
WireLogger.session.measureTime(label: "make client strategies", attributes: [.isNewClient: isNewClient]) {
660+
strategyDirectory.makeClientRelatedStrategies(
661+
applicationStatusDirectory: applicationStatusDirectory,
662+
syncContext: syncContext,
663+
transportSession: transportSession,
664+
pushMessageHandler: localNotificationDispatcher,
665+
flowManager: flowManager,
666+
incrementalSyncObserver: incrementalSyncObserver,
667+
metadata: resolvedBackendMetadata
668+
)
669+
}
666670
syncStrategy?.updateClientContextChangeTrackers()
667671
}
668672
Task {
@@ -1384,7 +1388,7 @@ extension ZMUserSession: ZMClientRegistrationStatusDelegate {
13841388
// The client was just registered and still needs to perform the
13851389
// initial sync.
13861390
if let selfClientID = userClient.remoteIdentifier {
1387-
setUpSyncAgent(clientID: selfClientID)
1391+
setUpSyncAgent(clientID: selfClientID, isNewClient: true)
13881392
// no migration needed from last sync system as it's a new client
13891393
if userClient.isConsumableNotificationsCapable {
13901394
// activate new sync with consumable notifications

β€Žwire-ios-sync-engine/Tests/Source/UserSession/ZMUserSessionTests.swiftβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ final class ZMUserSessionTests: ZMUserSessionTestsBase {
419419
sut.didRegisterSelfUserClient(selfUserClient)
420420
syncMOC.saveOrRollback()
421421

422-
sut.setUpSyncAgent(clientID: selfUserClient.remoteIdentifier!)
422+
sut.setUpSyncAgent(clientID: selfUserClient.remoteIdentifier!, isNewClient: false)
423423

424424
// WHEN
425425
sut.didFinishIncrementalSync(isRecovering: false)

0 commit comments

Comments
Β (0)