Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SCB-864] Add SQLTransport and JacksonSQLFormat #269

Merged
merged 3 commits into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public interface Operation {

String TYPE_NOP = "NOP";
String TYPE_REST = "rest";
String TYPE_SQL = "sql";
SagaResponse SUCCESSFUL_SAGA_RESPONSE = new SuccessfulSagaResponse("success");

SagaResponse send(String address);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.saga.core;

import java.util.Collections;
import java.util.List;

public class SQLOperation implements Operation {

private final String sql;
private final List<String> params;

public SQLOperation(String sql, List<String> params) {
this.sql = sql;
this.params = params == null ? Collections.<String>emptyList() : params;
}

public String sql() {
return sql;
}

public List<String> params() {
return params;
}

@Override
public String toString() {
return "SQLOperation{" +
"sql='" + sql + '\'' +
", params=" + params +
'}';
}

@Override
public SagaResponse send(String datasource) {
return SUCCESSFUL_SAGA_RESPONSE;
}

@Override
public SagaResponse send(String datasource, SagaResponse response) {
return send(datasource);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.saga.transports;

import java.util.List;

import org.apache.servicecomb.saga.core.SagaResponse;
import org.apache.servicecomb.saga.core.Transport;

public interface SQLTransport extends Transport {

SagaResponse with(String datasource, String sql, List<String> params);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.saga.format;

import java.util.List;

import org.apache.servicecomb.saga.core.Compensation;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class JacksonSQLCompensation extends JacksonSQLOperation implements Compensation {

private final int retries;

public JacksonSQLCompensation(String sql, List<String> params) {
this(sql, params, DEFAULT_RETRIES);
}

@JsonCreator
public JacksonSQLCompensation(
@JsonProperty("sql") String sql,
@JsonProperty("params") List<String> params,
@JsonProperty("retries") int retries) {
super(sql, params);
this.retries = retries <= 0? DEFAULT_RETRIES : retries;
}

@Override
public int retries() {
return this.retries;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.saga.format;

import java.util.List;

import org.apache.servicecomb.saga.core.Operation;
import org.apache.servicecomb.saga.core.SQLOperation;
import org.apache.servicecomb.saga.core.SagaResponse;
import org.apache.servicecomb.saga.transports.SQLTransport;
import org.apache.servicecomb.saga.transports.TransportFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;

public class JacksonSQLOperation extends SQLOperation implements TransportAware<SQLTransport> {

@JsonIgnore
private SQLTransport transport;

public JacksonSQLOperation(String sql, List<String> params) {
super(sql, params);
}

@Override
public Operation with(TransportFactory<SQLTransport> transport) {
this.transport = transport.getTransport();
return this;
}

@Override
public SagaResponse send(String datasource) {
return transport.with(datasource, sql(), params());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.saga.format;

import java.util.List;

import org.apache.servicecomb.saga.core.Transaction;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class JacksonSQLTransaction extends JacksonSQLOperation implements Transaction {

@JsonCreator
public JacksonSQLTransaction(
@JsonProperty("sql") String sql,
@JsonProperty("params") List<String> params) {
super(sql, params);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.servicecomb.saga.format;

import static org.apache.servicecomb.saga.format.JacksonFallback.NOP_TRANSPORT_AWARE_FALLBACK;

import org.apache.servicecomb.saga.core.Fallback;
import org.apache.servicecomb.saga.core.Operation;
import org.apache.servicecomb.saga.core.SagaRequestImpl;
import org.apache.servicecomb.saga.transports.SQLTransport;
import org.apache.servicecomb.saga.transports.TransportFactory;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class JsonSQLSagaRequest extends SagaRequestImpl implements JsonSagaRequest<SQLTransport> {

private JacksonSQLTransaction transaction;
private JacksonSQLCompensation compensation;

@JsonCreator
public JsonSQLSagaRequest(
@JsonProperty("id") String id,
@JsonProperty("datasource") String datasource,
@JsonProperty("type") String type,
@JsonProperty("transaction") JacksonSQLTransaction transaction,
@JsonProperty("compensation") JacksonSQLCompensation compensation,
@JsonProperty("fallback") Fallback fallback,
@JsonProperty("parents") String[] parents,
@JsonProperty("failRetryDelayMilliseconds") int failRetryDelayMilliseconds) {
super(id, datasource, type, transaction, compensation,
fallback == null ? NOP_TRANSPORT_AWARE_FALLBACK : fallback,
parents, failRetryDelayMilliseconds);

checkNull(transaction, "transaction");
checkNull(compensation, "compensation");

this.transaction = transaction;
this.compensation = compensation;
}

@Override
public JsonSagaRequest with(TransportFactory transportFactory) {
transaction.with(transportFactory);
compensation.with(transportFactory);
return this;
}

private void checkNull(Operation operation, String operationName) {
if (operation == null) {
throw new IllegalArgumentException("Invalid request with NO " + operationName + " specified");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
visible = true,
property = "type")
@JsonSubTypes({
@Type(value = JsonRestSagaRequest.class, name = Operation.TYPE_REST)
@Type(value = JsonRestSagaRequest.class, name = Operation.TYPE_REST),
@Type(value = JsonSQLSagaRequest.class, name = Operation.TYPE_SQL)
})
public interface JsonSagaRequest<T extends Transport> extends SagaRequest {

Expand Down