Skip to content

Commit

Permalink
Fix time consistency, use DB time where possible
Browse files Browse the repository at this point in the history
- Sets time zone on the database to UTC upon startup
- Makes Sequel interpret every timestamp as UTC
- Use Time.now.utc where DB time is not applicable

[#82077856]
  • Loading branch information
sujoybasu authored and Luan Santos and Sujoy Basu committed Jan 13, 2015
1 parent c75c99a commit eda570d
Show file tree
Hide file tree
Showing 57 changed files with 117 additions and 109 deletions.
2 changes: 1 addition & 1 deletion app/controllers/runtime/billing_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def end_time

def parse_date_param(param)
str = @params[param]
Time.parse(str).localtime if str
Time.parse(str).utc if str
rescue
raise Errors::ApiError.new_from_details('BillingEventQueryInvalid')
end
Expand Down
3 changes: 1 addition & 2 deletions app/jobs/runtime/pending_packages_cleanup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ module Jobs
module Runtime
class PendingPackagesCleanup < Struct.new(:expiration_in_seconds)
def perform
cutoff_time = Time.now - expiration_in_seconds
App.where('package_pending_since < ?', cutoff_time).update(
App.where("package_pending_since < ? - INTERVAL '?' SECOND", Sequel::CURRENT_TIMESTAMP, expiration_in_seconds.to_i).update(
package_state: 'FAILED',
staging_failed_reason: 'StagingTimeExpired',
package_pending_since: nil,
Expand Down
2 changes: 1 addition & 1 deletion app/models/runtime/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def mark_as_failed_to_stage(reason='StagingError')
def mark_for_restaging
self.package_state = 'PENDING'
self.staging_failed_reason = nil
self.package_pending_since = Time.now
self.package_pending_since = Sequel::CURRENT_TIMESTAMP
end

def buildpack
Expand Down
2 changes: 1 addition & 1 deletion app/models/runtime/app_start_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def event_type
def self.create_from_app(app)
return unless app.space.organization.billing_enabled?
AppStartEvent.create(
timestamp: Time.now,
timestamp: Sequel::CURRENT_TIMESTAMP,
organization_guid: app.space.organization_guid,
organization_name: app.space.organization.name,
space_guid: app.space.guid,
Expand Down
2 changes: 1 addition & 1 deletion app/models/runtime/app_stop_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def create_from_app(app)
end

AppStopEvent.create(
timestamp: Time.now,
timestamp: Sequel::CURRENT_TIMESTAMP,
organization_guid: app.space.organization_guid,
organization_name: app.space.organization.name,
space_guid: app.space.guid,
Expand Down
2 changes: 1 addition & 1 deletion app/models/runtime/organization_start_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def event_type
def self.create_from_org(org)
raise BillingNotEnabled unless org.billing_enabled?
OrganizationStartEvent.create(
timestamp: Time.now,
timestamp: Sequel::CURRENT_TIMESTAMP,
organization_guid: org.guid,
organization_name: org.name,
)
Expand Down
2 changes: 1 addition & 1 deletion app/models/services/service_create_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def self.create_from_service_instance(instance)

return unless org.billing_enabled?
ServiceCreateEvent.create(
timestamp: Time.now,
timestamp: Sequel::CURRENT_TIMESTAMP,
organization_guid: org.guid,
organization_name: org.name,
space_guid: space.guid,
Expand Down
2 changes: 1 addition & 1 deletion app/models/services/service_delete_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def self.create_from_service_instance(instance)

return unless org.billing_enabled?
ServiceDeleteEvent.create(
timestamp: Time.now,
timestamp: Sequel::CURRENT_TIMESTAMP,
organization_guid: org.guid,
organization_name: org.name,
space_guid: space.guid,
Expand Down
4 changes: 2 additions & 2 deletions app/presenters/api/job_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ def guid
end

def created_at
Time.at(0)
Time.at(0).utc
end

def run_at
Time.at(0)
Time.at(0).utc
end

def cf_api_error
Expand Down
2 changes: 1 addition & 1 deletion app/repositories/runtime/app_event_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def create_app_audit_event(type, app, space, actor, metadata)
Event.create(
space: space,
type: type,
timestamp: Time.now,
timestamp: Sequel::CURRENT_TIMESTAMP,
actee: app.guid,
actee_type: 'app',
actee_name: app.name,
Expand Down
6 changes: 3 additions & 3 deletions app/repositories/runtime/space_event_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def record_space_create(space, actor, actor_name, request_attrs)
actor: actor.guid,
actor_type: 'user',
actor_name: actor_name,
timestamp: Time.now,
timestamp: Sequel::CURRENT_TIMESTAMP,
metadata: {
request: request_attrs
}
Expand All @@ -29,7 +29,7 @@ def record_space_update(space, actor, actor_name, request_attrs)
actor: actor.guid,
actor_type: 'user',
actor_name: actor_name,
timestamp: Time.now,
timestamp: Sequel::CURRENT_TIMESTAMP,
metadata: {
request: request_attrs
}
Expand All @@ -45,7 +45,7 @@ def record_space_delete_request(space, actor, actor_name, recursive)
actor: actor.guid,
actor_type: 'user',
actor_name: actor_name,
timestamp: Time.now,
timestamp: Sequel::CURRENT_TIMESTAMP,
space_guid: space.guid,
organization_guid: space.organization.guid,
metadata: {
Expand Down
2 changes: 1 addition & 1 deletion app/repositories/services/event_repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def user_actor
def create_event(type, actor_data, actee_data, metadata, space_data=nil)
base_data = {
type: type,
timestamp: Time.now,
timestamp: Sequel::CURRENT_TIMESTAMP,
metadata: metadata
}

Expand Down
2 changes: 1 addition & 1 deletion db/migrations/20130219194917_create_stacks_table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
guid: SecureRandom.uuid,
name: 'lucid64',
description: 'Ubuntu 10.04 on x86-64',
created_at: Time.now,
created_at: Sequel::CURRENT_TIMESTAMP,
)

alter_table :apps do
Expand Down
2 changes: 2 additions & 0 deletions lib/cloud_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
require 'active_support/core_ext/object/to_query'
require 'active_support/json/encoding'

Sequel.default_timezone = :utc

module VCAP::CloudController; end

require 'vcap/errors/invalid_relation'
Expand Down
2 changes: 1 addition & 1 deletion lib/cloud_controller/blobstore/blob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def download_uri_for_file
end

if file.respond_to?(:url)
return file.url(Time.now + 3600)
return file.url(Time.now.utc + 3600)
end
file.public_url
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cloud_controller/blobstore/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def cp_r_to_blobstore(source_dir)
end

def cp_to_blobstore(source_path, destination_key, retries=2)
start = Time.now
start = Time.now.utc
logger.info('blobstore.cp-start', destination_key: destination_key, source_path: source_path, bucket: @directory_key)
size = -1
log_entry = 'blobstore.cp-skip'
Expand Down Expand Up @@ -78,7 +78,7 @@ def cp_to_blobstore(source_path, destination_key, retries=2)
log_entry = 'blobstore.cp-finish'
end

duration = Time.now - start
duration = Time.now.utc - start
logger.info(log_entry,
destination_key: destination_key,
duration_seconds: duration,
Expand Down
4 changes: 2 additions & 2 deletions lib/cloud_controller/clock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def start

def schedule_cleanup(name, klass, at)
Clockwork.every(1.day, "#{name}.cleanup.job", at: at) do |_|
@logger.info("Queueing #{klass} at #{Time.now}")
@logger.info("Queueing #{klass} at #{Time.now.utc}")
cutoff_age_in_days = @config.fetch(name.to_sym).fetch(:cutoff_age_in_days)
job = klass.new(cutoff_age_in_days)
Jobs::Enqueuer.new(job, queue: 'cc-generic').enqueue
Expand All @@ -32,7 +32,7 @@ def schedule_frequent_cleanup(name, klass)
config = @config.fetch(name.to_sym)

Clockwork.every(config.fetch(:frequency_in_seconds), "#{name}.cleanup.job") do |_|
@logger.info("Queueing #{klass} at #{Time.now}")
@logger.info("Queueing #{klass} at #{Time.now.utc}")
expiration = config.fetch(:expiration_in_seconds)
job = klass.new(expiration)
Jobs::Enqueuer.new(job, queue: 'cc-generic').enqueue
Expand Down
3 changes: 3 additions & 0 deletions lib/cloud_controller/db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def self.connect(opts, logger)

if db.database_type == :mysql
Sequel::MySQL.default_collate = 'utf8_bin'
db.run("SET time_zone = 'UTC'")
elsif db.database_type == :postgres
db.run("SET time zone 'UTC'")
end

db
Expand Down
4 changes: 2 additions & 2 deletions lib/cloud_controller/dea/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def find_all_instances(app)
unless all_instances[index]
all_instances[index] = {
state: 'DOWN',
since: Time.now.to_i,
since: Time.now.utc.to_i,
}
end
end
Expand Down Expand Up @@ -234,7 +234,7 @@ def find_stats(app)
unless stats[index]
stats[index] = {
state: 'DOWN',
since: Time.now.to_i,
since: Time.now.utc.to_i,
}
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/cloud_controller/dea/pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def register_subscriptions
end

def process_advertise_message(message)
advertisement = NatsMessages::DeaAdvertisement.new(message, Time.now.to_i + @advertise_timeout)
advertisement = NatsMessages::DeaAdvertisement.new(message, Time.now.utc.to_i + @advertise_timeout)

mutex.synchronize do
remove_advertisement_for_id(advertisement.dea_id)
Expand All @@ -30,7 +30,7 @@ def process_advertise_message(message)
end

def process_shutdown_message(message)
fake_advertisement = NatsMessages::DeaAdvertisement.new(message, Time.now.to_i + @advertise_timeout)
fake_advertisement = NatsMessages::DeaAdvertisement.new(message, Time.now.utc.to_i + @advertise_timeout)

mutex.synchronize do
remove_advertisement_for_id(fake_advertisement.dea_id)
Expand Down Expand Up @@ -69,7 +69,7 @@ def reserve_app_memory(dea_id, app_memory)
attr_reader :message_bus

def prune_stale_deas
now = Time.now.to_i
now = Time.now.utc.to_i
@dea_advertisements.delete_if { |ad| ad.expired?(now) }
end

Expand Down
4 changes: 2 additions & 2 deletions lib/cloud_controller/dea/stager_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(config, message_bus, blobstore_url_generator)
end

def process_advertise_message(msg)
advertisement = NatsMessages::StagerAdvertisement.new(msg, Time.now.to_i + @advertise_timeout)
advertisement = NatsMessages::StagerAdvertisement.new(msg, Time.now.utc.to_i + @advertise_timeout)
publish_buildpacks unless stager_in_pool?(advertisement.stager_id)

mutex.synchronize do
Expand Down Expand Up @@ -68,7 +68,7 @@ def top_5_stagers_for(memory, disk, stack)
end

def prune_stale_advertisements
now = Time.now.to_i
now = Time.now.utc.to_i
@stager_advertisements.delete_if { |ad| ad.expired?(now) }
end

Expand Down
6 changes: 3 additions & 3 deletions lib/cloud_controller/diagnostics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module VCAP::CloudController
class Diagnostics
def self.collect(output_directory)
data = {
time: Time.now,
time: Time.now.utc,
threads: thread_data,
varz: varz_data
}
Expand All @@ -27,7 +27,7 @@ def self.request_complete

def self.request_info(request)
{
start_time: Time.now.to_f,
start_time: Time.now.utc.to_f,
request_id: ::VCAP::Request.current_id,
request_method: request.request_method,
request_uri: request_uri(request)
Expand Down Expand Up @@ -57,7 +57,7 @@ def self.varz_data
end

def self.output_file_name
Time.now.strftime("diag-#{Process.pid}-%Y%m%d-%H:%M:%S.%L.json")
Time.now.utc.strftime("diag-#{Process.pid}-%Y%m%d-%H:%M:%S.%L.json")
end
end
end
2 changes: 1 addition & 1 deletion lib/cloud_controller/diego/instances_reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def fill_unreported_instances_with_down_instances(reported_instances, app)
unless reported_instances[i]
reported_instances[i] = {
state: 'DOWN',
since: Time.now.to_i,
since: Time.now.utc.to_i,
}
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/cloud_controller/diego/service_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def tps_addrs
attr_reader :message_bus

def set_tps_addr(guid, addr, ttl)
expires_at = Time.now + ttl
expires_at = Time.now.utc + ttl
tps_services[guid] = { addr: addr, expires_at: expires_at }
end

def expire_tps_addrs
now = Time.now
now = Time.now.utc
tps_services.select! { |_, val| val[:expires_at] > now }
end

Expand Down
4 changes: 2 additions & 2 deletions lib/cloud_controller/resource_pool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def overwrite_destination_with!(descriptor, destination)
logger.debug 'resource_pool.download.starting',
destination: destination

start = Time.now
start = Time.now.utc

if @cdn && @cdn[:uri]
logger.debug 'resource_pool.download.using-cdn'
Expand All @@ -146,7 +146,7 @@ def overwrite_destination_with!(descriptor, destination)
end
end

took = Time.now - start
took = Time.now.utc - start

logger.debug 'resource_pool.download.complete', took: took, destination: destination
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vcap/rest_api/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def clean_up_boolean(q_key, q_val)
end

def clean_up_datetime(q_val)
q_val.empty? ? nil : Time.parse(q_val).localtime
q_val.empty? ? nil : Time.parse(q_val).utc
end

def clean_up_integer(q_val)
Expand Down
4 changes: 2 additions & 2 deletions lib/vcap/uaa_token_decoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ def decode_token_with_asymmetric_key(auth_token)

def decode_token_with_key(auth_token, options)
options = { audience_ids: config[:resource_id] }.merge(options)
token = CF::UAA::TokenCoder.new(options).decode_at_reference_time(auth_token, Time.now.to_i - @grace_period_in_seconds)
token = CF::UAA::TokenCoder.new(options).decode_at_reference_time(auth_token, Time.now.utc.to_i - @grace_period_in_seconds)
expiration_time = token['exp'] || token[:exp]
if expiration_time && expiration_time < Time.now.to_i
if expiration_time && expiration_time < Time.now.utc.to_i
@logger.warn("token currently expired but accepted within grace period of #{@grace_period_in_seconds} seconds")
end
token
Expand Down
6 changes: 3 additions & 3 deletions spec/support/fakes/blueprints.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ module VCAP::CloudController
end

BillingEvent.blueprint do
timestamp { Time.now }
timestamp { Time.now.utc }
organization_guid { Sham.guid }
organization_name { Sham.name }
end

Event.blueprint do
timestamp { Time.now }
timestamp { Time.now.utc }
type { Sham.name }
actor { Sham.guid }
actor_type { Sham.name }
Expand Down Expand Up @@ -249,7 +249,7 @@ module VCAP::CloudController
instance_index { Sham.instance_index }
exit_status { Random.rand(256) }
exit_description { Sham.description }
timestamp { Time.now }
timestamp { Time.now.utc }
end

ServiceCreateEvent.blueprint do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/integration/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module IntegrationHttp
def admin_token
token = {
'aud' => 'cloud_controller',
'exp' => Time.now.to_i + 10_000,
'exp' => Time.now.utc.to_i + 10_000,
'client_id' => Sham.guid,
'scope' => ['cloud_controller.admin'],
}
Expand Down
Loading

0 comments on commit eda570d

Please sign in to comment.