Skip to content

Commit ce2bb74

Browse files
Fix code review remarks
1 parent 0b8a747 commit ce2bb74

File tree

4 files changed

+94
-166
lines changed

4 files changed

+94
-166
lines changed

.evergreen/config.yml

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -516,51 +516,6 @@ task_groups:
516516
$DRIVERS_TOOLS/.evergreen/atlas/teardown-atlas-cluster.sh
517517
tasks:
518518
- test-full-atlas-task
519-
520-
- name: test_aws_lambda_task_group
521-
setup_group_can_fail_task: true
522-
setup_group_timeout_secs: 1800 # 30 minutes
523-
setup_group:
524-
- func: fetch source
525-
- func: create expansions
526-
- command: shell.exec
527-
params:
528-
shell: "bash"
529-
working_dir: "src"
530-
script: |
531-
${PREPARE_SHELL}
532-
533-
echo "Setting up Atlas cluster"
534-
535-
DRIVERS_ATLAS_PUBLIC_API_KEY="${DRIVERS_ATLAS_PUBLIC_API_KEY}" \
536-
DRIVERS_ATLAS_PRIVATE_API_KEY="${DRIVERS_ATLAS_PRIVATE_API_KEY}" \
537-
DRIVERS_ATLAS_GROUP_ID="${DRIVERS_ATLAS_GROUP_ID}" \
538-
DRIVERS_ATLAS_LAMBDA_USER="${DRIVERS_ATLAS_LAMBDA_USER}" \
539-
DRIVERS_ATLAS_LAMBDA_PASSWORD="${DRIVERS_ATLAS_LAMBDA_PASSWORD}" \
540-
LAMBDA_STACK_NAME="dbx-ruby-lambda" \
541-
MONGODB_VERSION="7.0" \
542-
task_id="${task_id}" \
543-
execution="${execution}" \
544-
$DRIVERS_TOOLS/.evergreen/atlas/setup-atlas-cluster.sh
545-
- command: expansions.update
546-
params:
547-
file: src/atlas-expansion.yml
548-
teardown_group:
549-
- command: shell.exec
550-
params:
551-
shell: "bash"
552-
working_dir: "src"
553-
script: |
554-
${PREPARE_SHELL}
555-
556-
DRIVERS_ATLAS_PUBLIC_API_KEY="${DRIVERS_ATLAS_PUBLIC_API_KEY}" \
557-
DRIVERS_ATLAS_PRIVATE_API_KEY="${DRIVERS_ATLAS_PRIVATE_API_KEY}" \
558-
DRIVERS_ATLAS_GROUP_ID="${DRIVERS_ATLAS_GROUP_ID}" \
559-
LAMBDA_STACK_NAME="dbx-ruby-lambda" \
560-
task_id="${task_id}" \
561-
execution="${execution}" \
562-
$DRIVERS_TOOLS/.evergreen/atlas/teardown-atlas-cluster.sh
563-
tasks:
564519
- test-aws-lambda-deployed
565520

566521
- name: testgcpkms_task_group
@@ -1745,10 +1700,10 @@ buildvariants:
17451700
- name: testazurekms_task_group
17461701
batchtime: 20160 # Use a batchtime of 14 days as suggested by the CSFLE test README
17471702

1748-
- matrix_name: atlas-full
1703+
- matrix_name: atlas-full-and-lambda
17491704
matrix_spec:
17501705
ruby: "ruby-3.2"
1751-
os: rhel8
1706+
os: ubuntu2204
17521707
display_name: "Atlas (Full)"
17531708
tasks:
17541709
- name: testatlas_task_group
@@ -1771,11 +1726,3 @@ buildvariants:
17711726
display_name: "Atlas serverless ${ruby} single mongos"
17721727
tasks:
17731728
- name: test-serverless
1774-
1775-
- matrix_name: "aws-lambda"
1776-
matrix_spec:
1777-
ruby: "ruby-3.2"
1778-
os: ubuntu2204
1779-
display_name: "AWS Lambda"
1780-
tasks:
1781-
- name: test_aws_lambda_task_group

.evergreen/config/common.yml.erb

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -513,51 +513,6 @@ task_groups:
513513
$DRIVERS_TOOLS/.evergreen/atlas/teardown-atlas-cluster.sh
514514
tasks:
515515
- test-full-atlas-task
516-
517-
- name: test_aws_lambda_task_group
518-
setup_group_can_fail_task: true
519-
setup_group_timeout_secs: 1800 # 30 minutes
520-
setup_group:
521-
- func: fetch source
522-
- func: create expansions
523-
- command: shell.exec
524-
params:
525-
shell: "bash"
526-
working_dir: "src"
527-
script: |
528-
${PREPARE_SHELL}
529-
530-
echo "Setting up Atlas cluster"
531-
532-
DRIVERS_ATLAS_PUBLIC_API_KEY="${DRIVERS_ATLAS_PUBLIC_API_KEY}" \
533-
DRIVERS_ATLAS_PRIVATE_API_KEY="${DRIVERS_ATLAS_PRIVATE_API_KEY}" \
534-
DRIVERS_ATLAS_GROUP_ID="${DRIVERS_ATLAS_GROUP_ID}" \
535-
DRIVERS_ATLAS_LAMBDA_USER="${DRIVERS_ATLAS_LAMBDA_USER}" \
536-
DRIVERS_ATLAS_LAMBDA_PASSWORD="${DRIVERS_ATLAS_LAMBDA_PASSWORD}" \
537-
LAMBDA_STACK_NAME="dbx-ruby-lambda" \
538-
MONGODB_VERSION="7.0" \
539-
task_id="${task_id}" \
540-
execution="${execution}" \
541-
$DRIVERS_TOOLS/.evergreen/atlas/setup-atlas-cluster.sh
542-
- command: expansions.update
543-
params:
544-
file: src/atlas-expansion.yml
545-
teardown_group:
546-
- command: shell.exec
547-
params:
548-
shell: "bash"
549-
working_dir: "src"
550-
script: |
551-
${PREPARE_SHELL}
552-
553-
DRIVERS_ATLAS_PUBLIC_API_KEY="${DRIVERS_ATLAS_PUBLIC_API_KEY}" \
554-
DRIVERS_ATLAS_PRIVATE_API_KEY="${DRIVERS_ATLAS_PRIVATE_API_KEY}" \
555-
DRIVERS_ATLAS_GROUP_ID="${DRIVERS_ATLAS_GROUP_ID}" \
556-
LAMBDA_STACK_NAME="dbx-ruby-lambda" \
557-
task_id="${task_id}" \
558-
execution="${execution}" \
559-
$DRIVERS_TOOLS/.evergreen/atlas/teardown-atlas-cluster.sh
560-
tasks:
561516
- test-aws-lambda-deployed
562517

563518
- name: testgcpkms_task_group

.evergreen/config/standard.yml.erb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -494,10 +494,10 @@ buildvariants:
494494
- name: testazurekms_task_group
495495
batchtime: 20160 # Use a batchtime of 14 days as suggested by the CSFLE test README
496496

497-
- matrix_name: atlas-full
497+
- matrix_name: atlas-full-and-lambda
498498
matrix_spec:
499499
ruby: <%= latest_ruby %>
500-
os: rhel8
500+
os: ubuntu2204
501501
display_name: "Atlas (Full)"
502502
tasks:
503503
- name: testatlas_task_group
@@ -520,11 +520,3 @@ buildvariants:
520520
display_name: "Atlas serverless ${ruby} single mongos"
521521
tasks:
522522
- name: test-serverless
523-
524-
- matrix_name: "aws-lambda"
525-
matrix_spec:
526-
ruby: <%= latest_ruby %>
527-
os: ubuntu2204
528-
display_name: "AWS Lambda"
529-
tasks:
530-
- name: test_aws_lambda_task_group

spec/faas/ruby-sam-app/mongodb/app.rb

Lines changed: 90 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,113 +3,147 @@
33
require 'mongo'
44
require 'json'
55

6-
$open_connections = 0
7-
$heartbeats_count = 0
8-
$total_heartbeat_time = 0
9-
$commands_count = 0
10-
$total_command_time = 0
6+
class StatsAggregator
7+
8+
def initialize
9+
@open_connections = 0
10+
@heartbeats_count = 0
11+
@total_heartbeat_time = 0
12+
@commands_count = 0
13+
@total_command_time = 0
14+
end
15+
16+
def add_command(duration)
17+
@commands_count += 1
18+
@total_command_time += duration
19+
end
20+
21+
def add_heartbeat(duration)
22+
@heartbeats_count += 1
23+
@total_heartbeat_time += duration
24+
end
25+
26+
def add_connection
27+
@open_connections += 1
28+
end
29+
30+
def remove_connection
31+
@open_connections -= 1
32+
end
33+
34+
def average_heartbeat_time
35+
if @heartbeats_count == 0
36+
0
37+
else
38+
@total_heartbeat_time / @heartbeats_count
39+
end
40+
end
41+
42+
def average_command_time
43+
if @commands_count == 0
44+
0
45+
else
46+
@total_command_time / @commands_count
47+
end
48+
end
1149

50+
def reset
51+
@open_connections = 0
52+
@heartbeats_count = 0
53+
@total_heartbeat_time = 0
54+
@commands_count = 0
55+
@total_command_time = 0
56+
end
57+
58+
def result
59+
{
60+
average_heartbeat_time: average_heartbeat_time,
61+
average_command_time: average_command_time,
62+
heartbeats_count: @heartbeats_count,
63+
open_connections: @open_connections,
64+
}
65+
end
66+
end
1267

1368
class CommandMonitor
14-
def started(event)
69+
70+
def initialize(stats_aggregator)
71+
@stats_aggregator = stats_aggregator
1572
end
1673

74+
def started(event); end
75+
1776
def failed(event)
18-
$commands_count += 1
19-
$total_command_time += event.duration
77+
@stats_aggregator.add_command(event.duration)
2078
end
2179

2280
def succeeded(event)
23-
$commands_count += 1
24-
$total_command_time += event.duration
81+
@stats_aggregator.add_command(event.duration)
2582
end
2683
end
2784

2885
class HeartbeatMonitor
29-
def started(event)
86+
87+
def initialize(stats_aggregator)
88+
@stats_aggregator = stats_aggregator
3089
end
3190

91+
def started(event); end
92+
3293
def succeeded(event)
33-
$heartbeats_count += 1
34-
$total_heartbeat_time += event.duration
94+
@stats_aggregator.add_heartbeat(event.duration)
3595
end
3696

3797
def failed(event)
38-
$heartbeats_count += 1
39-
$total_heartbeat_time += event.duration
98+
@stats_aggregator.add_heartbeat(event.duration)
4099
end
41100
end
42101

43102
class PoolMonitor
103+
104+
def initialize(stats_aggregator)
105+
@stats_aggregator = stats_aggregator
106+
end
107+
44108
def published(event)
45109
case event
46110
when Mongo::Monitoring::Event::Cmap::ConnectionCreated
47-
$open_connections += 1
111+
@stats_aggregator.add_connection
48112
when Mongo::Monitoring::Event::Cmap::ConnectionClosed
49-
$open_connections -= 1
113+
@stats_aggregator.remove_connection
50114
end
51115
end
52116
end
53117

54-
command_monitor = CommandMonitor.new
55-
heartbeat_monitor = HeartbeatMonitor.new
56-
pool_monitor = PoolMonitor.new
118+
$stats_aggregator = StatsAggregator.new
119+
120+
command_monitor = CommandMonitor.new($stats_aggregator)
121+
heartbeat_monitor = HeartbeatMonitor.new($stats_aggregator)
122+
pool_monitor = PoolMonitor.new($stats_aggregator)
57123

58124
sdam_proc = proc do |client|
59125
client.subscribe(Mongo::Monitoring::COMMAND, command_monitor)
60126
client.subscribe(Mongo::Monitoring::SERVER_HEARTBEAT, heartbeat_monitor)
61127
client.subscribe(Mongo::Monitoring::CONNECTION_POOL, pool_monitor)
62128
end
63129

64-
$client = Mongo::Client.new(ENV['MONGODB_URI'], sdam_proc: sdam_proc)
65-
66130
puts 'Connecting'
131+
$client = Mongo::Client.new(ENV['MONGODB_URI'], sdam_proc: sdam_proc)
132+
# Populate the connection pool
67133
$client.use('lambda_test').database.list_collections
68134
puts 'Connected'
69135

70-
def reset_counters
71-
$heartbeats_count = 0
72-
$total_heartbeat_time = 0
73-
$commands_count = 0
74-
$total_command_time = 0
75-
$open_connections = 0
76-
end
77-
78-
def average_heartbeat_time
79-
if $heartbeats_count == 0
80-
0
81-
else
82-
$total_heartbeat_time / $heartbeats_count
83-
end
84-
end
85-
86-
def average_command_time
87-
if $commands_count == 0
88-
0
89-
else
90-
$total_command_time / $commands_count
91-
end
92-
end
93-
94136
def lambda_handler(event:, context:)
95137
db = $client.use('lambda_test')
96138
collection = db[:test_collection]
97139
result = collection.insert_one({ name: 'test' })
98140
collection.delete_one({ _id: result.inserted_id })
99-
response = {
100-
average_heartbeat_time: average_heartbeat_time,
101-
average_command_time: average_command_time,
102-
heartbeats_count: $heartbeats_count,
103-
open_connections: $open_connections,
104-
}.to_json
105-
reset_counters
141+
response = $stats_aggregator.result.to_json
142+
$stats_aggregator.reset
106143
puts "Response: #{response}"
107144

108145
{
109146
statusCode: 200,
110147
body: response
111148
}
112149
end
113-
114-
115-
lambda_handler(event: nil, context: nil)

0 commit comments

Comments
 (0)