Skip to content

Commit

Permalink
Merge branch 'release/2.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
tgianos committed Mar 17, 2015
2 parents d2f47e4 + 20b26b8 commit f5cb23a
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import com.netflix.genie.common.model.Job;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Set;

import com.netflix.genie.common.model.JobStatus;
import org.apache.commons.lang3.StringUtils;

/**
Expand Down Expand Up @@ -171,17 +171,17 @@ public Job waitForCompletion(final String id, final long blockTimeout, final lon

final long startTime = System.currentTimeMillis();

// wait for job to finish
while (true) {
final Job job = getJob(id);

// wait for job to finish - and finish time to be updated
if (!job.getFinished().equals(new Date(0))) {
final JobStatus status = job.getStatus();
if (status == JobStatus.FAILED || status == JobStatus.KILLED || status == JobStatus.SUCCEEDED) {
return job;
}

// block until timeout
long currTime = System.currentTimeMillis();
if (currTime - startTime < blockTimeout) {
if (System.currentTimeMillis() - startTime < blockTimeout) {
Thread.sleep(pollTime);
} else {
throw new InterruptedException("Timed out waiting for job to finish");
Expand Down
2 changes: 1 addition & 1 deletion genie-client/src/main/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
setup(
name='nflx-genie-client',

version='2.1.1',
version='2.1.2',

author='Netflix Inc.',

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
package com.netflix.genie.common.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.netflix.genie.common.exceptions.GeniePreconditionException;
import com.netflix.genie.common.util.JsonDateDeserializer;
import com.netflix.genie.common.util.JsonDateSerializer;
import com.wordnik.swagger.annotations.ApiModel;
import com.wordnik.swagger.annotations.ApiModelProperty;
Expand Down Expand Up @@ -58,6 +59,7 @@ public class Auditable implements Serializable, Validate {
* Unique ID.
*/
@Id
@Column(updatable = false)
@ApiModelProperty(
value = "The unique id of this resource. If one is not provided it is created internally"
)
Expand All @@ -68,12 +70,14 @@ public class Auditable implements Serializable, Validate {
*/
@Temporal(TemporalType.TIMESTAMP)
@Basic(optional = false)
@Column(updatable = false)
@ApiModelProperty(
value = "When this resource was created. Set automatically by system",
dataType = "date"
)
@JsonIgnore
private Date created;
@JsonSerialize(using = JsonDateSerializer.class)
@JsonDeserialize(using = JsonDateDeserializer.class)
private Date created = new Date();

/**
* The update timestamp.
Expand All @@ -84,8 +88,9 @@ public class Auditable implements Serializable, Validate {
value = "When this resource was last updated. Set automatically by system",
dataType = "date"
)
@JsonIgnore
private Date updated;
@JsonSerialize(using = JsonDateSerializer.class)
@JsonDeserialize(using = JsonDateDeserializer.class)
private Date updated = new Date();

/**
* The version of this entity. Auto handled by JPA.
Expand Down Expand Up @@ -146,49 +151,38 @@ public void setId(final String id) throws GeniePreconditionException {
*
* @return The created timestamps
*/
@JsonProperty("created")
@JsonSerialize(using = JsonDateSerializer.class)
public Date getCreated() {
if (this.created == null) {
return null;
} else {
return new Date(this.created.getTime());
}
return new Date(this.created.getTime());
}

/**
* Set the created timestamp. This is a No-Op. Set once by system.
*
* @param created The created timestamp
*/
@JsonIgnore
public void setCreated(final Date created) {
LOG.info("Tried to set created time to " + created + " for entity " + this.id + ". Ignoring.");
LOG.info("Tried to set created to " + created + " for entity " + this.id + ". Will not be persisted.");
if (created.before(this.created)) {
this.created = new Date(created.getTime());
}
}

/**
* Get the time this entity was updated.
*
* @return The updated timestamp
*/
@JsonProperty("updated")
@JsonSerialize(using = JsonDateSerializer.class)
public Date getUpdated() {
if (this.updated == null) {
return null;
} else {
return new Date(this.updated.getTime());
}
return new Date(this.updated.getTime());
}

/**
* Set the time this entity was updated. This is a No-Op. Updated automatically by system.
*
* @param updated The updated timestamp
*/
@JsonIgnore
public void setUpdated(final Date updated) {
LOG.info("Tried to set updated time to " + updated + " for entity " + this.id + ". Ignoring.");
this.updated = new Date(updated.getTime());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.text.DateFormat;
Expand All @@ -40,8 +41,15 @@ public class JsonDateDeserializer extends JsonDeserializer<Date> {
public Date deserialize(final JsonParser parser,
final DeserializationContext context) throws IOException {
final DateFormat format = new ISO8601DateFormat();

final String text = parser.getText();

if (StringUtils.isBlank(text)) {
return null;
}

try {
return format.parse(parser.getText());
return format.parse(text);
} catch (final ParseException pe) {
throw new IOException(pe);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public class JsonDateSerializer extends JsonSerializer<Date> {
@Override
public void serialize(final Date date, final JsonGenerator gen, final SerializerProvider provider)
throws IOException {
gen.writeString(ISO8601Utils.format(date));
if (date == null) {
gen.writeString((String) null);
} else {
gen.writeString(ISO8601Utils.format(date));
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class TestAuditable {
public void testConstructor() {
final Auditable a = new Auditable();
Assert.assertNull(a.getId());
Assert.assertNull(a.getCreated());
Assert.assertNull(a.getUpdated());
Assert.assertNotNull(a.getCreated());
Assert.assertNotNull(a.getUpdated());
}

/**
Expand Down Expand Up @@ -83,12 +83,17 @@ public void testSetIdTwice() throws GeniePreconditionException {
public void testOnCreateAuditable() throws InterruptedException, GeniePreconditionException {
final Auditable a = new Auditable();
Assert.assertNull(a.getId());
Assert.assertNull(a.getCreated());
Assert.assertNull(a.getUpdated());
Assert.assertNotNull(a.getCreated());
Assert.assertNotNull(a.getUpdated());
final Date originalCreated = a.getCreated();
final Date originalUpdated = a.getUpdated();
Thread.sleep(1);
a.onCreateAuditable();
Assert.assertNotNull(a.getId());
Assert.assertNotNull(a.getCreated());
Assert.assertNotNull(a.getUpdated());
Assert.assertNotEquals(originalCreated, a.getCreated());
Assert.assertNotEquals(originalUpdated, a.getUpdated());
Assert.assertEquals(a.getCreated(), a.getUpdated());

//Test to make sure if an ID already was set we don't change it
Expand All @@ -108,8 +113,8 @@ public void testOnCreateAuditable() throws InterruptedException, GeniePreconditi
public void testOnUpdateAuditable() throws InterruptedException {
final Auditable a = new Auditable();
Assert.assertNull(a.getId());
Assert.assertNull(a.getCreated());
Assert.assertNull(a.getUpdated());
Assert.assertNotNull(a.getCreated());
Assert.assertNotNull(a.getUpdated());
a.onCreateAuditable();
final Date originalCreate = a.getCreated();
final Date originalUpdate = a.getUpdated();
Expand All @@ -120,28 +125,38 @@ public void testOnUpdateAuditable() throws InterruptedException {
}

/**
* Test to make sure the setter of created does nothing.
* Test to make sure the setter of created does nothing relative to persistence.
*/
@Test
public void testSetCreated() {
final Auditable a = new Auditable();
Assert.assertNull(a.getCreated());
a.setCreated(new Date());
Assert.assertNull(a.getCreated());
Assert.assertNotNull(a.getCreated());
final Date date = new Date(0);
a.setCreated(date);
Assert.assertNotNull(a.getCreated());
Assert.assertEquals(date, a.getCreated());
a.onCreateAuditable();
Assert.assertNotNull(a.getCreated());
Assert.assertNotEquals(date, a.getCreated());
}

/**
* Test to make sure updated is never changed by set.
* Test to make sure updated is set but really is overwritten by onUpdate.
*/
@Test
public void testSetUpdated() {
public void testSetUpdated() throws InterruptedException {
final Auditable a = new Auditable();
Assert.assertNull(a.getUpdated());
final Date newer = new Date();
a.setUpdated(newer);
Assert.assertNull(a.getUpdated());
Assert.assertNotNull(a.getUpdated());
final Date date = new Date(0);
a.setUpdated(date);
Assert.assertNotNull(a.getUpdated());
Assert.assertEquals(date, a.getUpdated());
a.onCreateAuditable();
Assert.assertNotEquals(date, a.getUpdated());
final Date oldUpdated = a.getUpdated();
Thread.sleep(1);
a.onUpdateAuditable();
Assert.assertNotEquals(oldUpdated, a.getUpdated());
}

/**
Expand All @@ -166,18 +181,17 @@ public void testEntityVersion() {
public void testToString() throws GeniePreconditionException, IOException {
final Auditable a = new Auditable();
a.onCreateAuditable();
final String id = a.getId();
final Date created = a.getCreated();
final Date updated = a.getUpdated();

final String json = a.toString();

final ObjectMapper mapper = new ObjectMapper();
final Auditable b = mapper.readValue(json, Auditable.class);
Assert.assertEquals(id, b.getId());
Assert.assertNotEquals(created, b.getCreated());
Assert.assertNotEquals(updated, b.getUpdated());
Assert.assertNull(b.getCreated());
Assert.assertNull(b.getUpdated());
Assert.assertEquals(a.getId(), b.getId());

// Need to take off the milliseconds because they are lost anyway in the serialization/deserialization process
final long expectedCreated = a.getCreated().getTime() - (a.getCreated().getTime() % 1000L);
Assert.assertEquals(expectedCreated, b.getCreated().getTime());
final long expectedUpdated = a.getUpdated().getTime() - (a.getUpdated().getTime() % 1000L);
Assert.assertEquals(expectedUpdated, b.getUpdated().getTime());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ public void testDeserialize() throws IOException {
Assert.assertEquals(EXPECTED_MILLISECONDS, date.getTime());
}

/**
* Test the de-serialization method with null.
*
* @throws IOException
*/
@Test
public void testDeserializeNull() throws IOException {
Mockito.when(this.parser.getText()).thenReturn(null);

final JsonDateDeserializer deserializer = new JsonDateDeserializer();
final Date date = deserializer.deserialize(this.parser, this.context);
Assert.assertNull(date);
}

/**
* Test the de-serialization method.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,19 @@ public void testSerialize() throws IOException {
serializer.serialize(DATE, gen, provider);
Mockito.verify(gen, Mockito.times(1)).writeString(this.expectedString);
}

/**
* Test the serialization method with a null date.
*
* @throws IOException
*/
@Test
public void testSerializeNull() throws IOException {
final JsonGenerator gen = Mockito.mock(JsonGenerator.class);
final SerializerProvider provider = Mockito.mock(SerializerProvider.class);

final JsonDateSerializer serializer = new JsonDateSerializer();
serializer.serialize(null, gen, provider);
Mockito.verify(gen, Mockito.times(1)).writeString((String) null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.netflix.genie.server.services.ApplicationConfigService;
import com.netflix.genie.server.services.CommandConfigService;
import java.net.HttpURLConnection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -361,6 +362,27 @@ public void testUpdateApplicationWithId() throws GenieException {
Assert.assertEquals(6, updated.getTags().size());
}

/**
* Test to make sure setting the created and updated outside the system control doesn't change record in database.
*
* @throws GenieException
*/
@Test
public void testUpdateCreateAndUpdate() throws GenieException {
final Application init = this.service.getApplication(APP_1_ID);
final Date created = init.getCreated();
final Date updated = init.getUpdated();

init.setCreated(new Date());
final Date zero = new Date(0);
init.setUpdated(zero);

final Application updatedApp = this.service.updateApplication(APP_1_ID, init);
Assert.assertEquals(created, updatedApp.getCreated());
Assert.assertNotEquals(updated, updatedApp.getUpdated());
Assert.assertNotEquals(zero, updatedApp.getUpdated());
}

/**
* Test to update an application.
*
Expand Down
Loading

0 comments on commit f5cb23a

Please sign in to comment.