Skip to content

Commit 2438383

Browse files
authored
Lay the groundwork for CCT support, and include the prioritizer imple… (#2602)
* Lay the groundwork for CCT support, and include the prioritizer implementation. * Style * Adjust a flaky test. * Add GoogleDataTransportCCTSupport configs to travis * Fix small issues with travis configs * Fix syntax error in build.sh * Add SpecsStaging as a source for generation of GDTCCT * Address shortening warning * Remove headers that were unintentionally committed. * Make the podspec description != to the summary * Spelling
1 parent 47413cc commit 2438383

18 files changed

+641
-1
lines changed

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ jobs:
132132
- ./scripts/if_changed.sh ./scripts/build.sh $PROJECT $PLATFORM
133133
- ./scripts/if_changed.sh ./scripts/pod_lib_lint.sh GoogleDataTransport.podspec
134134

135+
# GoogleDataTransportCCTSupport unit tests and pod linting using the default Xcode version.
136+
- stage: test
137+
env:
138+
- PROJECT=GoogleDataTransportCCTSupport PLATFORM=iOS METHOD=xcodebuild
139+
before_install:
140+
- ./scripts/if_changed.sh ./scripts/install_prereqs.sh
141+
script:
142+
- ./scripts/if_changed.sh ./scripts/build.sh $PROJECT $PLATFORM
143+
- ./scripts/if_changed.sh ./scripts/pod_lib_lint.sh GoogleDataTransportCCTSupport.podspec
144+
135145
# Daily test for symbol collisions between Firebase and CocoaPods.
136146
- stage: test
137147
env:

GoogleDataTransport/Tests/Unit/GDTUploadCoordinatorTest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ - (void)testTimerIsRunningAtDesiredFrequency {
137137
// It's expected that the timer called the prioritizer 10 times +/- 3 during that 1 second + the
138138
// coordinator running before that.
139139
dispatch_sync([GDTUploadCoordinator sharedInstance].coordinationQueue, ^{
140-
XCTAssertEqualWithAccuracy(numberOfTimesCalled, 10, 3);
140+
XCTAssertGreaterThan(numberOfTimesCalled, 4); // Some latency is expected on a busy system.
141141
});
142142
}
143143

GoogleDataTransportCCTSupport.podspec

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
Pod::Spec.new do |s|
3+
s.name = 'GoogleDataTransportCCTSupport'
4+
s.version = '0.1.0'
5+
s.summary = 'Support library for the GoogleDataTransport CCT log target.'
6+
7+
8+
s.description = <<-DESC
9+
Support library to provide event prioritization and uploading for the GoogleDataTransport CCT log target.
10+
DESC
11+
12+
s.homepage = 'https://developers.google.com/'
13+
s.license = { :type => 'Apache', :file => 'LICENSE' }
14+
s.authors = 'Google, Inc.'
15+
s.source = {
16+
:git => 'https://github.com/firebase/firebase-ios-sdk.git',
17+
:tag => 'GoogleDataLoggerCCTSupport-' + s.version.to_s
18+
}
19+
20+
s.ios.deployment_target = '8.0'
21+
22+
s.cocoapods_version = '>= 1.6.0'
23+
24+
s.static_framework = true
25+
s.prefix_header_file = false
26+
27+
s.source_files = 'GoogleDataTransportCCTSupport/GoogleDataTransportCCTSupport/Classes/**/*'
28+
s.private_header_files = 'GoogleDataTransportCCTSupport/GoogleDataTransportCCTSupport/Classes/Private/*.h'
29+
30+
s.dependency 'GoogleDataTransport'
31+
s.dependency 'nanopb'
32+
33+
s.pod_target_xcconfig = {
34+
'GCC_C_LANGUAGE_STANDARD' => 'c99',
35+
'GCC_TREAT_WARNINGS_AS_ERRORS' => 'YES',
36+
'CLANG_UNDEFINED_BEHAVIOR_SANITIZER_NULLABILITY' => 'YES',
37+
'GCC_PREPROCESSOR_DEFINITIONS' =>
38+
# The nanopb pod sets these defs, so we must too. (We *do* require 16bit
39+
# (or larger) fields, so we'd have to set at least PB_FIELD_16BIT
40+
# anyways.)
41+
'PB_FIELD_32BIT=1 PB_NO_PACKED_STRUCTS=1 PB_ENABLE_MALLOC=1',
42+
}
43+
44+
# Test specs
45+
s.test_spec 'Tests-Unit' do |test_spec|
46+
test_spec.requires_app_host = false
47+
test_spec.source_files = 'GoogleDataTransportCCTSupport/Tests/Unit/**/*.{h,m}'
48+
end
49+
50+
end

GoogleDataTransportCCTSupport/CHANGELOG.md

Whitespace-only changes.
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "GDTCCTPrioritizer.h"
18+
19+
const static NSUInteger kMillisPerDay = 8.64e+7;
20+
21+
@implementation GDTCCTPrioritizer
22+
23+
+ (void)load {
24+
GDTCCTPrioritizer *prioritizer = [GDTCCTPrioritizer sharedInstance];
25+
[[GDTRegistrar sharedInstance] registerPrioritizer:prioritizer target:kGDTTargetCCT];
26+
}
27+
28+
/** Creates and returns the singleton instance of this class.
29+
*
30+
* @return The singleton instance of this class.
31+
*/
32+
+ (instancetype)sharedInstance {
33+
static GDTCCTPrioritizer *sharedInstance;
34+
static dispatch_once_t onceToken;
35+
dispatch_once(&onceToken, ^{
36+
sharedInstance = [[GDTCCTPrioritizer alloc] init];
37+
});
38+
return sharedInstance;
39+
}
40+
41+
- (instancetype)init {
42+
self = [super init];
43+
if (self) {
44+
_queue = dispatch_queue_create("com.google.GDTCCTPrioritizer", DISPATCH_QUEUE_SERIAL);
45+
_events = [[NSMutableSet alloc] init];
46+
}
47+
return self;
48+
}
49+
50+
#pragma mark - GDTPrioritizer Protocol
51+
52+
- (void)prioritizeEvent:(GDTStoredEvent *)event {
53+
dispatch_async(_queue, ^{
54+
[self.events addObject:event];
55+
});
56+
}
57+
58+
- (void)unprioritizeEvents:(NSSet<GDTStoredEvent *> *)events {
59+
dispatch_async(_queue, ^{
60+
for (GDTStoredEvent *event in events) {
61+
[self.events removeObject:event];
62+
}
63+
});
64+
}
65+
66+
- (GDTUploadPackage *)uploadPackageWithConditions:(GDTUploadConditions)conditions {
67+
GDTUploadPackage *package = [[GDTUploadPackage alloc] init];
68+
dispatch_sync(_queue, ^{
69+
NSSet<GDTStoredEvent *> *logEventsThatWillBeSent;
70+
if ((conditions & GDTUploadConditionWifiData) == GDTUploadConditionWifiData) {
71+
logEventsThatWillBeSent = [self logEventsOkToSendOnWifi];
72+
} else {
73+
logEventsThatWillBeSent = [self logEventsOkToSendOnMobileData];
74+
}
75+
if (self.timeOfLastDailyUpload) {
76+
int64_t millisSinceLastUpload =
77+
[GDTClock snapshot].timeMillis - self.timeOfLastDailyUpload.timeMillis;
78+
if (millisSinceLastUpload > kMillisPerDay) {
79+
logEventsThatWillBeSent =
80+
[logEventsThatWillBeSent setByAddingObjectsFromSet:[self logEventsOkToSendDaily]];
81+
}
82+
} else {
83+
self.timeOfLastDailyUpload = [GDTClock snapshot];
84+
logEventsThatWillBeSent =
85+
[logEventsThatWillBeSent setByAddingObjectsFromSet:[self logEventsOkToSendDaily]];
86+
}
87+
package.events = logEventsThatWillBeSent;
88+
});
89+
return package;
90+
}
91+
92+
#pragma mark - Private helper methods
93+
94+
/** The different possible quality of service specifiers. High values indicate high priority. */
95+
typedef NS_ENUM(NSInteger, GDTCCTQoSTier) {
96+
/** The QoS tier wasn't set, and won't ever be sent. */
97+
GDTCCTQoSDefault = 0,
98+
99+
/** This event is internal telemetry data that should not be sent on its own if possible. */
100+
GDTCCTQoSTelemetry = 1,
101+
102+
/** This event should be sent, but in a batch only roughly once per day. */
103+
GDTCCTQoSDaily = 2,
104+
105+
/** This event should only be uploaded on wifi. */
106+
GDTCCTQoSWifiOnly = 5,
107+
};
108+
109+
/** Converts a GDTEventQoS to a GDTCCTQoS tier.
110+
*
111+
* @param qosTier The GDTEventQoS value.
112+
* @return A static NSNumber that represents the CCT QoS tier.
113+
*/
114+
FOUNDATION_STATIC_INLINE
115+
NSNumber *GDTCCTQosTierFromGDTEventQosTier(GDTEventQoS qosTier) {
116+
switch (qosTier) {
117+
case GDTEventQoSTelemetry:
118+
case GDTEventQoSWifiOnly:
119+
return @(GDTCCTQoSWifiOnly);
120+
break;
121+
122+
case GDTEventQoSDaily:
123+
return @(GDTCCTQoSDaily);
124+
break;
125+
126+
default:
127+
return @(GDTCCTQoSDefault);
128+
break;
129+
}
130+
}
131+
132+
/** Returns a set of logs that are ok to upload whilst on mobile data.
133+
*
134+
* @note This should be called from a thread safe method.
135+
* @return A set of logs that are ok to upload whilst on mobile data.
136+
*/
137+
- (NSSet<GDTStoredEvent *> *)logEventsOkToSendOnMobileData {
138+
return
139+
[self.events objectsPassingTest:^BOOL(GDTStoredEvent *_Nonnull event, BOOL *_Nonnull stop) {
140+
return [GDTCCTQosTierFromGDTEventQosTier(event.qosTier) isEqual:@(GDTCCTQoSDefault)];
141+
}];
142+
}
143+
144+
/** Returns a set of logs that are ok to upload whilst on wifi.
145+
*
146+
* @note This should be called from a thread safe method.
147+
* @return A set of logs that are ok to upload whilst on wifi.
148+
*/
149+
- (NSSet<GDTStoredEvent *> *)logEventsOkToSendOnWifi {
150+
return
151+
[self.events objectsPassingTest:^BOOL(GDTStoredEvent *_Nonnull event, BOOL *_Nonnull stop) {
152+
NSNumber *qosTier = GDTCCTQosTierFromGDTEventQosTier(event.qosTier);
153+
return [qosTier isEqual:@(GDTCCTQoSDefault)] || [qosTier isEqual:@(GDTCCTQoSWifiOnly)];
154+
}];
155+
}
156+
157+
/** Returns a set of logs that only should have a single upload attempt per day.
158+
*
159+
* @note This should be called from a thread safe method.
160+
* @return A set of logs that are ok to upload only once per day.
161+
*/
162+
- (NSSet<GDTStoredEvent *> *)logEventsOkToSendDaily {
163+
return
164+
[self.events objectsPassingTest:^BOOL(GDTStoredEvent *_Nonnull event, BOOL *_Nonnull stop) {
165+
return [GDTCCTQosTierFromGDTEventQosTier(event.qosTier) isEqual:@(GDTCCTQoSDaily)];
166+
}];
167+
}
168+
169+
@end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import "GDTCCTUploader.h"
18+
19+
#import <GoogleDataTransport/GDTRegistrar.h>
20+
21+
@implementation GDTCCTUploader
22+
23+
+ (void)load {
24+
GDTCCTUploader *uploader = [GDTCCTUploader sharedInstance];
25+
[[GDTRegistrar sharedInstance] registerUploader:uploader target:kGDTTargetCCT];
26+
}
27+
28+
+ (instancetype)sharedInstance {
29+
static GDTCCTUploader *sharedInstance;
30+
static dispatch_once_t onceToken;
31+
dispatch_once(&onceToken, ^{
32+
sharedInstance = [[GDTCCTUploader alloc] init];
33+
});
34+
return sharedInstance;
35+
}
36+
37+
- (void)uploadPackage:(nonnull GDTUploadPackage *)package
38+
onComplete:(nonnull GDTUploaderCompletionBlock)onComplete {
39+
}
40+
41+
@end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#import <GoogleDataTransport/GoogleDataTransport.h>
20+
21+
/** Manages the prioritization of events from GoogleDataTransport. */
22+
@interface GDTCCTPrioritizer : NSObject <GDTPrioritizer>
23+
24+
/** The queue on which this prioritizer operates. */
25+
@property(nonatomic) dispatch_queue_t queue;
26+
27+
/** All log events that have been processed by this prioritizer. */
28+
@property(nonatomic) NSMutableSet<GDTStoredEvent *> *events;
29+
30+
/** The most recent attempted upload of daily uploaded logs. */
31+
@property(nonatomic) GDTClock *timeOfLastDailyUpload;
32+
33+
/** Creates and/or returns the singleton instance of the prioritizer. */
34+
+ (instancetype)sharedInstance;
35+
36+
@end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2019 Google
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#import <Foundation/Foundation.h>
18+
19+
#import <GoogleDataTransport/GoogleDataTransport.h>
20+
21+
/** Class capable of uploading events to the CCT backend. */
22+
@interface GDTCCTUploader : NSObject <GDTUploader>
23+
24+
/** Creates/returns the single instance. */
25+
+ (instancetype)sharedInstance;
26+
27+
@end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
To build the protos:
2+
- `brew intall protobuf`
3+
- Download the latest stable release from https://github.com/nanopb/nanopb/releases
4+
- `./build_protos.sh <path to nanopb download>`
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Google Data Transport CCT Support Lib
2+
3+
This library is for internal Google use only. It allows the logging of data and
4+
telemetry from Google SDKs.
5+
6+
## Prereqs
7+
8+
- Install [cocoapod-generate](https://github.com/square/cocoapods-generate)
9+
10+
## To develop
11+
12+
- Run `generate_project.sh` after installing the prereqs

0 commit comments

Comments
 (0)