Created
October 23, 2015 20:18
-
-
Save robhruska/071d4468a3223d649291 to your computer and use it in GitHub Desktop.
UserMigrationState.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The MIT License (MIT) | |
Copyright (c) 2015 Agile Sports Technologies, Inc. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
using Hudl.Config; | |
using log4net; | |
namespace Hudl.Users.Migration.Control | |
{ | |
// Singleton state used to decide how to handle an operation for a specific user ID, where the user | |
// may or may not be migrated. State mutation is done by an external migration job, interacting with | |
// this singleton via HTTP APIs. | |
public class UserMigrationState | |
{ | |
public static readonly UserMigrationState Instance = new UserMigrationState(); | |
// We'll enable new user creates in mongo before beginning the migration, starting at a known id. | |
public static readonly IConfigurableValue<bool> CreateNewUsersInMongo = new ConfigurableValue<bool>("users.migration.createNewUsersInMongo", false); | |
public static readonly IConfigurableValue<int> NewUserMongoIdStart = new ConfigurableValue<int>("users.migration.newUserMongoIdStart", 6000000); | |
private static readonly ILog Log = LogManager.GetLogger(typeof (UserMigrationState)); | |
public long MigrationPoint { get { return _migrationPoint; } } | |
public long LockedBatchLength { get { return _lockedBatchLength; } } | |
// All users below this (exclusive) are in mongo. This and above are in SQL. | |
private long _migrationPoint = 0; | |
// The number of user ids above the migration point (inclusive) that are locked. | |
private long _lockedBatchLength = 0; | |
public bool IsUserMigrated(string userId) | |
{ | |
var longUserId = long.Parse(userId); | |
if (CreateNewUsersInMongo.Value && longUserId >= NewUserMongoIdStart.Value) | |
{ | |
return true; | |
} | |
return longUserId < _migrationPoint; | |
} | |
// Called by all SQL write methods to guarantee we don't try to write to the old SQL | |
// data for already-migrated users. | |
public void ThrowIfUserIsMigrated(string userId) | |
{ | |
if (IsUserMigrated(userId)) | |
{ | |
throw new MigratedUserOldDatabaseException(userId); | |
} | |
} | |
public void ThrowIfUserIsMigrated(long userId) | |
{ | |
ThrowIfUserIsMigrated(userId.ToString()); | |
} | |
// Called by all write methods (both SQL and MongoDB) to prevent writes on users in the | |
// currently-active migration range. | |
public void ThrowIfUserIsLocked(string userId) | |
{ | |
if (IsUserLocked(userId)) | |
{ | |
throw new LockedUserException(_migrationPoint, _lockedBatchLength, userId); | |
} | |
} | |
public bool IsUserLocked(string userId) | |
{ | |
// If _lockedBatchLength is 0, we should have no locked users. | |
var longUserId = long.Parse(userId); | |
return _migrationPoint <= longUserId && longUserId < (_migrationPoint + _lockedBatchLength); | |
} | |
public bool ShouldNewUsersBeCreatedInMongo() | |
{ | |
return CreateNewUsersInMongo.Value; | |
} | |
// Mutates the state. Called only by the interactive console application that controls the job, | |
// and on application startup to retrieve any persisted state and maintain it across app restarts). | |
public void SetPointAndBatch(long point, long batch) | |
{ | |
Log.InfoFormat("Setting Point={0} and Batch={1}", point, batch); | |
_migrationPoint = point; | |
_lockedBatchLength = batch; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment