AWS create RetryPolicy with multiple RetryConditions - aws-sdk

I am creating a client for DynamoDB and I would like to create RetryPolicy multiple RetryCondition. Is there a way to do that?
Following example has a retry client with one RetryPolicy:
ClientOverrideConfiguration.builder()
.retryPolicy(RetryPolicy.builder().retryCondition(RetryOnExceptionsCondition.create(UnknownHostException.class))
.build();

RetryOnExceptionsCondition::create method takes vararg, a collection of exceptions that can be used in a RetryPolicy.
RetryOnExceptionsCondition.create(UnknownHostException.class, SdkException.class)

Related

need help withself reference loop when serialize linq

I know this question was asked before but I couldn't find any solution for my issue.
I am developing a WebAPI with more than 10 Controllers which their methods access a server DB.
I am using Linq2SQL to write the queries and using Json to serialize the return to send it back to my application.
The problem is no matter how simple is the query it returns the self reference loop when serialize and this is happening in all controller methods. See one example below:
var retitems = dtcxapi.ListItems.AsQueryable()
.Where(i => i.IsActive == true && i.ListName.ToLower() == listName.ToLower()).ToList();
where dtcxapi is my DataContext and ListItems is my table.
When serialize it shows: Self referencing loop detected with type 'BV.IMSWEBAPI.User'. Path '[0].User1.Users1'.
But as I said this error will occur for any query in any controller methods. I tried already to use the ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore in mWeb config but it didn't fix.
Any help will be really appreciated.
Thanks
The only true way to fix this is to not return your Linq objects and instead return a DTO/Model that is not tied to your database. If you are returning your database objects, you will always run into self referencing loops because of Navigation properties.
You haven't mentioned if you are using .NET Core or .NET Framework, but if it's Core, it won't use web.config at all and instead you should modify your startup method :
services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
But again, this is a bandaid, the correct solution is to use DTOs
More info : https://dotnetcoretutorials.com/2020/03/15/fixing-json-self-referencing-loop-exceptions/

How to call MySQL stored procedure from ASP.NET Core 3.1 MySql.Data.EntityFrameworkCore

I have a stored procedure in Mysql that returns a table.
Using of old methods like _context.database.SqlQuery doesn't work anymore.
The _context.database.Execute* only contains methods that returns number of affected rows.
In my scenario I'm using scaffolding and can't create database objects from my code, but I can create classes.
The following code (and/or similar tries with Set or Query that is obsolete)
_context.Set<MyModel>().FromSql("CALL My_USP({0});", parametervalue).ToList<MyModel>();
returns an error about that the model is not in the _context because I'm scaffolding and MyModel is a class from my Models.
I'm totally lost with this and all help I can find in S.O. or Google are about EF6, that doesn't work in my case, the libraries are different.
Any workaround will be appreciated also, if this is not possible to do.
I got a solution but I will mark as answer someone that works without the old ADO.NET or change my dbcontext like this one, because will fail the next time I will do a scaffolding.
Add to protected override void OnModelCreating(ModelBuilder modelBuilder) in the context file:
modelBuilder.Entity<MyModel>().HasNoKey();
and then call:
_context.Set<MyModel>().FromSqlRaw("CALL My_USP({0});", parametervalue).ToList<MyModel>();

Can't create instance from java sdk

I am trying to create an OCI instance with the Java SDK. I am getting an error in routeRules.
When I commented the following line, I was able to create an instance. But in that machines route is not enabled.
addInternetGatewayToRouteTable(vcnClient, compartmentId, vcn.getDefaultRouteTableId(), internetGateway);
https://github.com/oracle/oci-java-sdk/blob/master/bmc-examples/src/main/java/CreateInstanceExample.java
Exception in thread "main" com.oracle.bmc.model.BmcException: (400, InvalidParameter, false) routeRules[0].networkEntityId may not be null (opc-request-id: 6BC8A182852240F8AFFD1EB279CFF901/AD5BF82603D64DA298976FCFE83871F6/9398B04078B0435A8FA68FEA7307CC99)
at com.oracle.bmc.http.internal.ResponseHelper.throwIfNotSuccessful(ResponseHelper.java:120)
at com.oracle.bmc.http.internal.ResponseConversionFunctionFactory$ValidatingParseResponseFunction.apply(ResponseConversionFunctionFactory.java:86)
at com.oracle.bmc.http.internal.ResponseConversionFunctionFactory$ValidatingParseResponseFunction.apply(ResponseConversionFunctionFactory.java:82)
at com.oracle.bmc.core.internal.http.UpdateRouteTableConverter$1.apply(UpdateRouteTableConverter.java:70)
at com.oracle.bmc.core.internal.http.UpdateRouteTableConverter$1.apply(UpdateRouteTableConverter.java:55)
at com.oracle.bmc.core.VirtualNetworkClient.updateRouteTable(VirtualNetworkClient.java:3325)
at CreateInstanceExample.addInternetGatewayToRouteTable(CreateInstanceExample.java:295)
at CreateInstanceExample.main(CreateInstanceExample.java:146)
There is a bug in the code, you can replace destination in the addInternetGatewayToRouteTable(..) method
RouteRule internetAccessRoute =
RouteRule.builder()
.cidrBlock("0.0.0.0/0")
.destination(internetGateway.getId())
.build();
in line 295 with networkEntityId:
RouteRule internetAccessRoute =
RouteRule.builder()
.cidrBlock("0.0.0.0/0")
.networkEntityId(internetGateway.getId())
.build();
You can see from the REST APIs here which parameters are required to create a route rule in a route table.
The example seems to be based on a deprecated version of calling the RouteRule builder.

How to schedule Laravel 5 job to get data from external JSON file, and store value in database?

I'm currently working on a project in Laravel, and I want to schedule a job that grabs a value (the price of Bitcoin) from an external API (JSON file) and stores this value in my database every few minutes.
So far, I have created a job using the artisan command: artisan make:job UpdateBitcoinMarketPrice. But I've no idea what to include in my public function handle() in side of the Job class that was created.
I have fathomed that I can call this job regularly from App\Console\Kernel.php with the following function:
protected function schedule(Schedule $schedule){
// $schedule->command('inspire')
// ->hourly();
$schedule->job(new UpdateBitcoinMarketPrice)->everyFiveMinutes();}
Should I, for example, create a new Model that stores said value? Then create a new Object every-time this run?
Should I then call the first row of the database should I wish to return the value?
Job classes are very simple, normally containing only a handle() method which is called when the job is processed by the queue. You can use the contructor to inject any parameter or serialize a model so you can use it in your handle method.
So to keep it bold you can make the api call on the handle method and store the response in the databse. Knowing that this is going to fire the api call as a background job.
Something along the lines of:
public function __construct(User $user)
{
//In this case Laravel serilizes the User model example so you could use it on your background job.
//This can be anything that you need in order to make the call
$this->user = $user;
}
//Injecting as an example ExtrernalServieClass or Transformer(to transform api response).
public function handle(ExternalServiceClass $service, Transformer $transform)
{
//Here you can make the call to the api.
//Get the response parse it
// Store to database
$response = $service->postRequest($someUri, $someParams);
$parsedResponse = $transform->serviceResponse($response);
DatabaseModel::firstOrCreate($parsedResponse);
}
}
The handle method is called when the job is processed by the queue. Note that you are able to type-hint dependencies on the handle method of the job, like in the example above. The Laravel service container automatically injects these dependencies.
Now since you are going to run the job everyFiveMinutes() you have to be careful since if the previous job is not completed by default, scheduled tasks will be run even if the previous instance of the task is still running.
To prevent this, you may use the withoutOverlapping method:
$schedule->job(new UpdateBitcoinMarketPrice)->everyFiveMinutes()->>withoutOverlapping();

Returning values from InputFormat via the Hadoop Configuration object

Consider a running Hadoop job, in which a custom InputFormat needs to communicate ("return", similarly to a callback) a few simple values to the driver class (i.e., to the class that has launched the job), from within its overriden getSplits() method, using the new mapreduce API (as opposed to mapred).
These values should ideally be returned in-memory (as opposed to saving them to HDFS or to the DistributedCache).
If these values were only numbers, one could be tempted to use Hadoop counters. However, in numerous tests counters do not seem to be available at the getSplits() phase and anyway they are restricted to numbers.
An alternative could be to use the Configuration object of the job, which, as the source code reveals, should be the same object in memory for both the getSplits() and the driver class.
In such a scenario, if the InputFormat wants to "return" a (say) positive long value to the driver class, the code would look something like:
// In the custom InputFormat.
public List<InputSplit> getSplits(JobContext job) throws IOException
{
...
long value = ... // A value >= 0
job.getConfiguration().setLong("value", value);
...
}
// In the Hadoop driver class.
Job job = ... // Get the job to be launched
...
job.submit(); // Start running the job
...
while (!job.isComplete())
{
...
if (job.getConfiguration().getLong("value", -1))
{
...
}
else
{
continue; // Wait for the value to be set by getSplits()
}
...
}
The above works in tests, but is it a "safe" way of communicating values?
Or is there a better approach for such in-memory "callbacks"?
UPDATE
The "in-memory callback" technique may not work in all Hadoop distributions, so, as mentioned above, a safer way is, instead of saving the values to be passed back in the Configuration object, create a custom object, serialize it (e.g., as JSON), saved it (in HDFS or in the distributed cache) and have it read in the driver class. I have also tested this approach and it works as expected.
Using the configuration is a perfectly suitable solution (admittedly for a problem I'm not sure I understand), but once the job has actually been submitted to the Job tracker, you will not be able to amend this value (client side or task side) and expect to see the change on the opposite side of the comms (setting configuration values in a map task for example will not be persisted to the other mappers, nor to the reducers, nor will be visible to the job tracker).
So to communicate information back from within getSplits back to your client polling loop (to see when the job has actually finished defining the input splits) is fine in your example.
What's your greater aim or use case for using this?