Skip to content

Commit

Permalink
Initial commit for user assignment lookup table
Browse files Browse the repository at this point in the history
  • Loading branch information
mans4singh committed Nov 23, 2016
1 parent be1e450 commit 079400a
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 240 deletions.
14 changes: 9 additions & 5 deletions bin/docker/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ For example, to run the cqlsh container using docker, assuming the Cassandra is
docker run -it --rm -e CASSANDRA_KEYSPACE_PREFIX=wasabi -e CQLSH_HOST=wasabi-cassandra -e CASSANDRA_PORT=9042 --network=wasabi_nw --name wasabi_create_keyspace felixgao/wasabi_keyspace:1.0.0
```

## Important Notes:
- The migration scripts do not save and restore data in the tables being modified.
- The migration cql script names indicate the nature of the mutation involved in that step - eg: Create, Drop, etc
- Destructive cql that have ```Drop``` in the name will drop the table and data in that table will be lost. If such data is required, please save it before executing the migration script and reimport it into the new table as required.

##Migration

To create the docker image that is need for migration, you may use the following command
```
docker build -t felixgao/wasabi-migration:1.0.0 -f migration.docker ../../
```
The above command assumes you are in the $ProjectDir/bin/docker directory. You may change the docker build context as you see fit.

####Environment Variables
- CASSANDRA_KEYSPACE
* default: wasabi_experiments
Expand All @@ -52,12 +56,12 @@ docker run -it --rm -e CASSANDRA_KEYSPACE_PREFIX=wasabi -e CQLSH_HOST=wasabi-cas
* default: 9042
- CASSANDRA_MIGRATION
# default: /wasabi/cassandra-migration.jar

For example, to run the wasabi-migration container using docker, assuming the Cassandra is running on a host called wasabi-cassandra and listens on port 9042. You may omit --network option if the cassandra node is not managed by docker.
```
docker run -it --rm -e CQLSH_HOST=wasabi-cassandra -e CASSANDRA_PORT=9042 --network=wasabi_nw --name wasabi_schema_migration felixgao/wasabi-migration:1.0.0
```

## Local Cassandra

if you are running the migration against local installation on the mac or your ip is not publicly reachable. You will have to ran the script manually by providing the above environment variables using the ```migration.sh``` directly.
Expand All @@ -68,4 +72,4 @@ for example, if your Mac OSx is running the cassandra locally, you will need the
export CASSANDRA_MIGRATION=/local_location_of_the_migration_tool.jar
export MIGRATION_SCRIPT=/location_of_where_the_cql_scripts
bin/docker/migration.sh
```
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create table user_assignment_by_userId_context_experimentId (
experiment_id uuid,
user_id varchar,
context varchar,
bucket_label varchar,
created timestamp,
PRIMARY KEY (user_id, context, experiment_id)
);
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected void configure() {
bind(StateExperimentIndexAccessor.class).toProvider(StateExperimentIndexAccessorProvider.class).in(Singleton.class);
bind(UserAssignmentIndexAccessor.class).toProvider(UserAssignmentIndexAccessorProvider.class).in(Singleton.class);
bind(UserBucketIndexAccessor.class).toProvider(UserBucketIndexAccessorProvider.class).in(Singleton.class);
bind(UserExperimentIndexAccessor.class).toProvider(UserExperimentIndexAccessorProvider.class).in(Singleton.class);
// bind(UserExperimentIndexAccessor.class).toProvider(UserExperimentIndexAccessorProvider.class).in(Singleton.class);
//Bind those audit
bind(AuditLogAccessor.class).toProvider(AuditLogAccessorProvider.class).in(Singleton.class);
bind(BucketAuditLogAccessor.class).toProvider(BucketAuditLogAccessorProvider.class).in(Singleton.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public interface UserAssignmentIndexAccessor {
* @param bucketLabel
* @return resultSet
*/
@Query("insert into user_assignment_look_up (experiment_id, user_id, context, created, bucket_label)" +
@Query("insert into user_assignment_by_userId_context_experimentId (experiment_id, user_id, context, created, bucket_label)" +
" values (?, ?, ?, ?, ?)")
ResultSet insertBy(UUID uuid, String userId, String context, Date created, String bucketLabel);

Expand All @@ -51,7 +51,7 @@ public interface UserAssignmentIndexAccessor {
* @param created
* @return resultSet
*/
@Query("insert into user_assignment_look_up (experiment_id, user_id, context, created)" +
@Query("insert into user_assignment_by_userId_context_experimentId (experiment_id, user_id, context, created)" +
" values (?, ?, ?, ?)")
ResultSet insertBy(UUID uuid, String userId, String context, Date created);

Expand All @@ -62,7 +62,7 @@ public interface UserAssignmentIndexAccessor {
* @param context
* @return result
*/
@Query("select * from user_assignment_look_up where experiment_id = ? and user_id = ? and context = ?")
@Query("select * from user_assignment_by_userId_context_experimentId where experiment_id = ? and user_id = ? and context = ?")
Result<UserAssignmentByUserIdContextExperimentId> selectBy(UUID experimentId, String userId, String context);


Expand All @@ -73,7 +73,7 @@ public interface UserAssignmentIndexAccessor {
* @param experimentId
* @return resultSet
*/
@Query("delete from user_assignment_look_up where user_id = ? and context = ? and experiment_id = ?")
@Query("delete from user_assignment_by_userId_context_experimentId where user_id = ? and context = ? and experiment_id = ?")
ResultSet deleteBy(String userId, String context, UUID experimentId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,44 @@
/**
* Accessor for user experiment index table
*/
@Accessor
public interface UserExperimentIndexAccessor {

/**
* Insert row into table
* @param userId
* @param context
* @param appName
* @param experimentId
* @param bucketLabel
* @return result set
*/
@Query("insert into user_experiment_index (user_id, context, app_name, experiment_id, bucket_label)" +
" values (?, ?, ?, ?, ?)")
ResultSet insertBy(String userId, String context, String appName, UUID experimentId, String bucketLabel);

/**
* Insert row into table based on attributes
* @param userId
* @param context
* @param appName
* @param experimentId
* @return result set
*/
@Query("insert into user_experiment_index (user_id, context, app_name, experiment_id)" +
" values (?, ?, ?, ?)")
ResultSet insertBy(String userId, String context, String appName, UUID experimentId);


/**
* Delete row from table
* @param userId
* @param experimentId
* @param context
* @param appName
* @return result set
*/
@Query("delete from user_experiment_index where user_id = ? and experiment_id = ? and context = ? and app_name = ?")
ResultSet deleteBy(String userId, UUID experimentId, String context, String appName);

}
//@Accessor
//public interface UserExperimentIndexAccessor {
//
// /**
// * Insert row into table
// * @param userId
// * @param context
// * @param appName
// * @param experimentId
// * @param bucketLabel
// * @return result set
// */
// @Query("insert into user_experiment_index (user_id, context, app_name, experiment_id, bucket_label)" +
// " values (?, ?, ?, ?, ?)")
// ResultSet insertBy(String userId, String context, String appName, UUID experimentId, String bucketLabel);
//
// /**
// * Insert row into table based on attributes
// * @param userId
// * @param context
// * @param appName
// * @param experimentId
// * @return result set
// */
// @Query("insert into user_experiment_index (user_id, context, app_name, experiment_id)" +
// " values (?, ?, ?, ?)")
// ResultSet insertBy(String userId, String context, String appName, UUID experimentId);
//
//
// /**
// * Delete row from table
// * @param userId
// * @param experimentId
// * @param context
// * @param appName
// * @return result set
// */
// @Query("delete from user_experiment_index where user_id = ? and experiment_id = ? and context = ? and app_name = ?")
// ResultSet deleteBy(String userId, UUID experimentId, String context, String appName);
//
//}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import com.intuit.wasabi.repository.cassandra.accessor.index.ExperimentUserIndexAccessor;
import com.intuit.wasabi.repository.cassandra.accessor.index.UserAssignmentIndexAccessor;
import com.intuit.wasabi.repository.cassandra.accessor.index.UserBucketIndexAccessor;
import com.intuit.wasabi.repository.cassandra.accessor.index.UserExperimentIndexAccessor;
//import com.intuit.wasabi.repository.cassandra.accessor.index.UserExperimentIndexAccessor;
import com.intuit.wasabi.repository.cassandra.pojo.UserAssignment;
import com.intuit.wasabi.repository.cassandra.pojo.export.UserAssignmentExport;
import com.intuit.wasabi.repository.cassandra.pojo.index.ExperimentUserByUserIdContextAppNameExperimentId;
Expand Down Expand Up @@ -86,7 +86,7 @@ public class CassandraAssignmentsRepository implements AssignmentsRepository {

private ExperimentAccessor experimentAccessor;
private ExperimentUserIndexAccessor experimentUserIndexAccessor;
private UserExperimentIndexAccessor userExperimentIndexAccessor;
// private UserExperimentIndexAccessor userExperimentIndexAccessor;

private UserAssignmentAccessor userAssignmentAccessor;
private UserAssignmentIndexAccessor userAssignmentIndexAccessor;
Expand All @@ -106,7 +106,7 @@ public CassandraAssignmentsRepository(
EventLog eventLog,
ExperimentAccessor experimentAccessor,
ExperimentUserIndexAccessor experimentUserIndexAccessor,
UserExperimentIndexAccessor userExperimentIndexAccessor,
// UserExperimentIndexAccessor userExperimentIndexAccessor,

UserAssignmentAccessor userAssignmentAccessor,
UserAssignmentIndexAccessor userAssignmentIndexAccessor,
Expand Down Expand Up @@ -138,7 +138,7 @@ public CassandraAssignmentsRepository(
//Experiment related accessors
this.experimentAccessor = experimentAccessor;
this.experimentUserIndexAccessor = experimentUserIndexAccessor;
this.userExperimentIndexAccessor = userExperimentIndexAccessor;
// this.userExperimentIndexAccessor = userExperimentIndexAccessor;
//UserAssignment related accessors
this.userAssignmentAccessor = userAssignmentAccessor;
this.userAssignmentIndexAccessor = userAssignmentIndexAccessor;
Expand Down Expand Up @@ -345,7 +345,7 @@ public Assignment assignUser(Assignment assignment, Experiment experiment, Date
assignmentsCountExecutor.execute(new AssignmentCountEnvelope(this, experimentRepository,
dbRepository, experiment, assignment, countUp, eventLog, date, assignUserToExport, assignBucketCount));

indexUserToExperiment(assignment);
// indexUserToExperiment(assignment);
indexUserToBucket(assignment);
indexExperimentsToUser(assignment);

Expand Down Expand Up @@ -399,29 +399,29 @@ void indexUserToBucket(Assignment assignment) {
}
}

void indexUserToExperiment(Assignment assignment) {
try {
if(Objects.isNull(assignment.getBucketLabel())) {
userExperimentIndexAccessor.insertBy(
assignment.getUserID().toString(),
assignment.getContext().getContext(),
assignment.getApplicationName().toString(),
assignment.getExperimentID().getRawID(),
new String(new byte[0], StandardCharsets.UTF_8 ) //Needed because of compact storage, which is essentially just ""
);
} else {
userExperimentIndexAccessor.insertBy(
assignment.getUserID().toString(),
assignment.getContext().getContext(),
assignment.getApplicationName().toString(),
assignment.getExperimentID().getRawID(),
assignment.getBucketLabel().toString()
);
}
} catch (WriteTimeoutException | UnavailableException | NoHostAvailableException e){
throw new RepositoryException("Could not index user to experiment \"" + assignment + "\"", e);
}
}
// void indexUserToExperiment(Assignment assignment) {
// try {
// if(Objects.isNull(assignment.getBucketLabel())) {
// userExperimentIndexAccessor.insertBy(
// assignment.getUserID().toString(),
// assignment.getContext().getContext(),
// assignment.getApplicationName().toString(),
// assignment.getExperimentID().getRawID(),
// new String(new byte[0], StandardCharsets.UTF_8 ) //Needed because of compact storage, which is essentially just ""
// );
// } else {
// userExperimentIndexAccessor.insertBy(
// assignment.getUserID().toString(),
// assignment.getContext().getContext(),
// assignment.getApplicationName().toString(),
// assignment.getExperimentID().getRawID(),
// assignment.getBucketLabel().toString()
// );
// }
// } catch (WriteTimeoutException | UnavailableException | NoHostAvailableException e){
// throw new RepositoryException("Could not index user to experiment \"" + assignment + "\"", e);
// }
// }

/**
* Adds an assignment associated with a new user
Expand Down Expand Up @@ -535,7 +535,7 @@ public void deleteAssignment(Experiment experiment, User.ID userID, Context cont
dbRepository, experiment, currentAssignment, countUp, eventLog, null, assignUserToExport,
assignBucketCount));

removeIndexUserToExperiment(userID, experiment.getID(), context, appName);
// removeIndexUserToExperiment(userID, experiment.getID(), context, appName);
removeIndexUserToBucket(userID, experiment.getID(), context, currentAssignment.getBucketLabel());
removeIndexExperimentsToUser(userID, experiment.getID(), context, appName);
}
Expand Down Expand Up @@ -577,20 +577,20 @@ void deleteUserFromLookUp(Experiment.ID experimentID, User.ID userID, Context co
* @param context context
* @param appName application name
*/
public void removeIndexUserToExperiment(User.ID userID, Experiment.ID experimentID, Context context,
Application.Name appName) {
try {
userExperimentIndexAccessor.deleteBy(
userID.toString(),
experimentID.getRawID(),
context.getContext(),
appName.toString()
);
} catch (WriteTimeoutException | UnavailableException | NoHostAvailableException e) {
throw new RepositoryException(
"Could not remove from user_experiment_index for user: " + userID + " to experiment: " + experimentID, e);
}
}
// public void removeIndexUserToExperiment(User.ID userID, Experiment.ID experimentID, Context context,
// Application.Name appName) {
// try {
// userExperimentIndexAccessor.deleteBy(
// userID.toString(),
// experimentID.getRawID(),
// context.getContext(),
// appName.toString()
// );
// } catch (WriteTimeoutException | UnavailableException | NoHostAvailableException e) {
// throw new RepositoryException(
// "Could not remove from user_experiment_index for user: " + userID + " to experiment: " + experimentID, e);
// }
// }

@Override
public void removeIndexUserToBucket(User.ID userID, Experiment.ID experimentID, Context context, Bucket.Label bucketLabel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


//TODO: this seems redundent since UserAssignment contains the same data with same primary key but different order, do we make queries that are order specific?
@Table(name="user_assignment_look_up")
@Table(name="user_assignment_by_userId_context_experimentId")
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,27 @@

import java.util.UUID;

@Table(name="user_experiment_index")
@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
public class UserExperimentByAppNameUserIdContextExperimentId {
@PartitionKey(0)
@Column(name = "app_name")
String appName;

@PartitionKey(1)
@Column(name = "user_id")
String userId;

@PartitionKey(2)
String context;

@PartitionKey(3)
@Column(name = "experiment_id")
UUID experimentId;

@Column(name = "bucket_label")
String bucketLabel;
}
//@Table(name="user_experiment_index")
//@Data
//@Builder(toBuilder = true)
//@NoArgsConstructor
//@AllArgsConstructor
//public class UserExperimentByAppNameUserIdContextExperimentId {
// @PartitionKey(0)
// @Column(name = "app_name")
// String appName;
//
// @PartitionKey(1)
// @Column(name = "user_id")
// String userId;
//
// @PartitionKey(2)
// String context;
//
// @PartitionKey(3)
// @Column(name = "experiment_id")
// UUID experimentId;
//
// @Column(name = "bucket_label")
// String bucketLabel;
//}
Loading

0 comments on commit 079400a

Please sign in to comment.