java – RxJava and race conditions

I have seen some RxJava projects using multithreading with .subscribeOn(Schedulers.io()), and their .subscribe(() -> {...}) code is manipulating some data structures that are also processed by other Observable subscription handlers, and there are no synchronized() anywhere inside subscribe.

Coming from C/C++, I started to wonder – wait, if those subscribes happen to execute on different threads, then what about race conditions?

In C/C++ it would crash and burn with some access violation sooner or later. Of course, Java seems to be much more tolerant. But what if two subscribes happen to work with the same linked list in parallel? Won’t they mess up the links between nodes? Shouldn’t I protect them with synchronized()?

I searched for articles about RxJava and race conditions, but didn’t find much. One article claims the following:

So, whenever you want to switch threads for any particular observable, all you need to do is specify the observeOn() operator just above it. Synchronization, state inconsistencies, race conditions, and all other threading edge cases are automatically handled under the hood.

Is it true and RxJava indeed somehow automagically protects its subscribes for race conditions with other subscribes working on the same data? Or is the author talking only about race conditions with RxJava itself or specifically about observeOn and not subscribeOn?

google bigquery – Calculate the total on events with two time conditions

I have a table in BigQuery that looks something like this:

    schema = (
        bigquery.SchemaField('timestamp', 'TIMESTAMP', mode='REQUIRED', description='Data point timestamp'),
        bigquery.SchemaField('event_id', 'STRING', description='EventID'),
        (...)
    )

The table has a fairly large dataset, and I’m trying to find write an efficient query that returns the number of events that happened in the last 24 hours but also within the last N days. That is, two different records with different conditions but the same event_id. I don’t care so much about the actual event_id, but rather the distribution.

Ideally, the query would return something like this:

7_days: 20
30_days: 15
60_days: 7

If it’s impossible to do this in pure SQL, I also have Pandas available at my disposal.

Poisson Equation with variable coefficients and mixed boundary conditions

I try to solve the Poisson equation with variable coefficients with mixed boundary condition in 2D((2,3)x(2,3), I did:

 ClearAll(y, x1, x2);
  a = x1 + x2;
  pde = D(a D(y(x1, x2), x1), x1) + D(a D(y(x1, x2), x2), x2) - 2;
  bc = {y(x1, 2) == 2 + x1, y(x1, 3) == 3 + x1};
  sol = NDSolve({pde == 
  NeumannValue(-1*a, x1 == 2) + NeumannValue(1*a, x1 == 3), bc}, 
    y, {x1, 2, 3}, {x2, 2, 3})

  Plot3D(Evaluate(y(x1, x2) /. sol), {x1, 2, 3}, {x2, 2, 3}, 
  PlotRange -> All, AxesLabel -> {"x1", "X2", "y(x1,x2)"}, 
  BaseStyle -> 12)

The exact solution y=x1+x2, however, the results not provide high accuracy.

python – When to extract boolean conditions into their own function?

I commonly use a boolean condition/variable when it’s something too big or complicated and takes too much space by itself inside ifs – basically to avoid repeatability and thus improve readability. E.g. has_three_repeated_digits = len(some_number) == 4 and len(set(some_number(1:)) and set(some_number(:-1))) != 1 and ... etc. So it is used as if has_three_repeated_digits:...

However, I’ve noticed it is quite common to use boolean-returning functions instead:

def has_three_repeated_digits(number):
    return len(number) == 4  # ... etc 

And it is used as if has_three_repeated_digits(some_number): ...

So, when should I turn my boolean variable has_foo into a function has_foo(bar)? My initial suspicion is that I should do it whenever this variable will be used inside another method, or at a similar situation (as making a boolean variable global would be quite… ugly?). But I’m not sure.

postgresql – Return single row with one to many relationship based on evaluated conditions

Database is Postgres.

I have an issue whereby I have a table with the following columns: ID, Product_ID Customer_ID, Price and Item_Type.

This table is joined on Product_ID to another table. For a Given product ID there is the default price (where the Customer_ID is Blank) for a given item type (New, Renewal, Redemption etc.), there may also be a customer specific price list (where Customer_ID is joined to the Customers Table).

The issue is that this Database has been poorly maintained. For Item_Type there are 2 Prices that for this issue we are interested: Renew and Renewal. Some prices have a Renew price, some have a Renewal Price and some have both.

select p.id, pr.customer_id, p.period, pr.price, pr.item_type
from products p
right join prices pr on
pr.product_id = p.id and pr.period = p.period and pr.customer_id is null
where p.status = 'PURCHASED'

As an example this produces the following:

121751  8407508 12 29.95 RENEW
121751  8407508 12 35.95 RENEWAL

The cause is that this product has 2 different prices for Renew and Renewal – looking at the front end application code – if there is an item_type ‘RENEWAL’ then this price is used, if not then the ‘RENEW’ price is used.

I’ve tried using a case statement, both in the select and in a where clause:

case pr.item_type 
    when 'RENEWAL' then pr.price
    when 'RENEW' then pr.price
    end

But I still get both rows – I’m not sure how to do a conditional and hierarchical lookup e.g. if Condition A is matched, then return row that matches Condition A, if no match is found, Condition B is matched, then return row that matches Condition B etc.

ag.algebraic geometry – $mathscr{M}_*$, the stack of generalized elliptic curves (with some additional conditions) is locally of finite presentaion

Let $mathscr{M}_*$ be the stack over $mathbb{Z}$ which classifies generalized elliptic curves $E/S$, such that for every geometric point $k$ of $mathbb{Z}$, the fibre $E_k$ is smooth or $n$-gon, where $mathrm{char} k$ does not divide $n$.
(see III.0. of Deligne-Rapoport’s “Les schemas de modules de courbes elliptiques”)

In III.2.5., the authors claim that $mathscr{M}_*$ is locally of finite presentation as a fibred category, i.e., for any direct limit of rings $A = lim A_i$, the canonical map $lim mathscr{M}_* (A_i) to mathscr{M}_*(A)$ is an equivalence.
They say that this is EGA IV.8.8.

Using it (and some propositions in Stack Project), we can show, for a generalized elliptic curve $E/A$, that there exist an $i$, a proper flat curve $E_i / A_i$, and morphism $E_i^text{sm} times E_i to E_i$, which induce the ones of $E/A$.
But I can’t show that for some $i$, this $E_i/A_i$ is a generalized elliptic cuvre.

If for some $i$ $E_i/A_i$ has reduced geometric fibres, then we can show that this $E_i/A_i$ has connected geometric fibres.
And it seems that I can show for some $i$, every geometric fibre of $E_i/A_i$ has the trivial dualizing sheaf.
(First assume $A_i$ noether. Then $operatorname{Spce} A_i$ has an connected open subset which intersects the image of $operatorname{Spec}A$.
Thus using the Picard scheme of $E_i times U / U$, we can show the subset “${omega = mathscr{O} }$” of $U$ is clopen.)

So finally, I want to show that for some $i$, every geometric fibre of $E_i/A_i$ is reduced, and whose singularity is at least ordinary double.
(If so, then by the argument above and II.1.13., we have that this $E_i/A_i$ is a generalized elliptic curve.)

I can’t find it in EGA.
So please suggest me some references of it, or please prove it.

differential equations – Slow Solution from DSolve when Adding Initial Conditions to system of ODEs

it is having hard time solving for $c_i$, from initial conditions. We can do it manually

ode1 = uB'(t) == -a*uB(t) *uC(t) + b*uD(t);
ode2 = uC'(t) == -a*uB(t) *uC(t) + b*uD(t);
ode3 = uD'(t) == a*uB(t) *uC(t) - b*uD(t);
ic = {uB(0) == 1, uC(0) == 1, uD(0) == 0};
exactSol = 
 First@DSolve({ode1, ode2, ode3}, {uB(t), uC(t), uD(t)}, t) // 
  FullSimplify

enter image description here

eq1 = uB(t) /. exactSol
eq2 = uC(t) /. exactSol
eq3 = uD(t) /. exactSol

enter image description here

sol = Solve({1 == eq1 /. t -> 0, 1 == eq2 /. t -> 0, 
   0 == eq3 /. t -> 0}, {C(1), C(2), C(3)})

enter image description here

 sol = First@Normal(sol, ConditionalExpression)

enter image description here

 exactSol /. sol

enter image description here

Notice, the solutions are not unique!. There is arbitrary $c_4$ in them. You set this to any value in Z (I take this back. Any integer value for $c_4$ gives the same solution. So any integer value for C(4) will work).

 exactSol /. sol/. C(4)->1

enter image description here

Verify against numerical

Assuming(uB > 0 && uC > 0 && uD > 0, 
 NumericalSol = 
  First@NDSolve({uB'(t) == -uB(t) uC(t) + uD(t), 
     uC'(t) == -uB(t) uC(t) + uD(t), uD'(t) == uB(t) uC(t) - uD(t), 
     uB(0) == 1, uC(0) == 1, uD(0) == 0}, {uB, uC, uD}, {t, 0, 1}));
Plot(Evaluate({uB(t), uC(t), uD(t)} /. NumericalSol), {t, 0, 1})

enter image description here

newSol = exactSol /. sol /. C(4) -> 1;
Plot(Evaluate({uB(t), uC(t), uD(t)} /. newSol /. {a -> 1, 
    b -> 1}), {t, 0, 1})

enter image description here

apache kafka – How to deal with time-based conditions in an event-driven architecture?

The company I work for has a ticketing platform. The backend system consists of a few applications talking to each other via HTTP and through events in Kafka. A ticket has a sell_start_date field indicating when it can start being sold. We have to develop an application exposes available tickets as a HTTP service. For performance reasons, we have to keep the tickets in memory. There is a topic in Kafka containing ticket CRUD information. So when a ticket is created in the backoffice, an event is published to this topic.

Our new application could consume all of these events to populate its in-memory cache. The problem is that we don’t want to store tickets with sell_start_date far in the future. But if, when consuming an event, we discard it based on this, we’ll lose it as it won’t be published again. Periodically reprocessing the topic is not an option because the topic’s retention time is 7 days and the tickets might be created much earlier.

The only option that we can think of is having job that, instead of reading from Kafka, periodically reads from the DB where we keep all tickets. This creates some other complications that we’d like to avoid. It’d be great if we could somehow leverage the event-driven architecture to also include time-based events, not having to use periodic jobs.

This, having time-based events/conditions, seems like a common problem. How are people solving it?

manipulation – How do you rearrange a list of strings based given certain conditions?

I had looked at previous answers for this but was unable to find a solution. I have the following code:

dnames1={"A1","A2","A3","A4","A5","A6"}
dnumber={20,20,11,21,19,20}

newdnumber=TakeSmallest(dnumber,6)


AssociationThread(dnames,dnumber)

The positions of the strings given in “dnames1” correspond to the values in the same position given by list “dnumber” (this can be seen through the AssociationThread). I then rearranged the list “dnumber” to go from the smallest number to the largest, giving a new list designated “newdnumber.” I’m trying to then produce a new corresponding list called “newdnames1” which reflects the new ordering given by “newdnumber.” This would give an output of:

newdnames1={"A3","A5","A1","A2","A6","A4"}

I’ve tried the following to solve this question:

assoc=AssociationThread(dnumber,dnames1)
newdnumber/.assoc

but the main issue is that the AssociationThreaddoes not associate the correct list of strings if there are repeating values given in dnumber (I.e. The number 20 appears 3 times but the association only ever assigns “A6″`a value of 20 and it never assigns strings “A1” or “A2”. Any help producing newdnames1 would be greatly appreciated.

cancellations – Under what conditions can travellers from Germany be reimbursed in case of a travel warning?

I’m not too sure about nuances about Urlauber in German, but if it is referring to customers of packaged holidays, the EU Package Travel Directive and its German implementation (§§651a ff. BGB) apply.

In that case, §651h BGB provides for

(3) Abweichend von Absatz 1 Satz 3 kann der Reiseveranstalter keine Entschädigung verlangen, wenn am Bestimmungsort oder in dessen unmittelbarer Nähe unvermeidbare, außergewöhnliche Umstände auftreten, die die Durchführung der Pauschalreise oder die Beförderung von Personen an den Bestimmungsort erheblich beeinträchtigen. Umstände sind unvermeidbar und außergewöhnlich im Sinne dieses Untertitels, wenn sie nicht der Kontrolle der Partei unterliegen, die sich hierauf beruft, und sich ihre Folgen auch dann nicht hätten vermeiden lassen, wenn alle zumutbaren Vorkehrungen getroffen worden wären.

(3) Notwithstanding the third sentence in Absatz 1, the tour operator may not claim compensation if unavoidable, exceptional circumstances arise at or in the immediate vicinity of the destination which significantly affect the performance of the package or the transport of persons to the destination. Circumstances are unavoidable and exceptional within the meaning of this Subtitle if they are beyond the control of the party invoking them and their consequences could not have been avoided even if all reasonable precautions had been taken.

Travel warnings are indicative (“in der Regel”) of an unavoidable and exceptional circumstances that affects the performance/execution of the travel package.

Reisewarnungen stellen aber ohnehin nur ein Indiz dar, sind also nicht allein ausschlaggebend für die Beantwortung der Frage, ob eine Pauschalreise kostenlos storniert werden kann oder nicht.

Travel warnings are only an indication anyway and are therefore not the only decisive factor in answering the question of whether a package tour can be canceled free of charge or not.

https://www.bmjv.de/DE/Themen/FokusThemen/Corona/Reiserecht/Corona_Reiserecht_node.html

Further rights to refund exist for general reasons of impossibility to perform a contract or receive a service/goods due to restrictions by the German government (but travel warning from the Foreign Office is not a travel ban).

  1. Sie haben eine Pauschalreise gebucht? In folgenden Fällen entstehen keine Stornokosten

Sie können zurücktreten, wenn im Reiseland sogenannte außergewöhnliche Umstände vorliegen, also zum Beispiel Krankheiten wie Covid19: Ein wichtiges Indiz sind Reisewarnungen des Auswärtigen Amtes. Aktuell gibt es eine Reisewarnung für alle Länder mit Ausnahme der Mitgliedsstaaten der Europäischen Union, der Schengen-assoziierten Staaten und des Vereinigten Königreiches – vorerst bis einschließlich 31. August. (…)

  1. Was bedeutet das für Buchungen von Hotels oder Ferienhäusern?

Können Sie Ihr Ziel aufgrund der Reisebeschränkungen der deutschen Regierung, eines Einreiseverbots oder der Lage der Unterkunft im Sperrgebiet nicht erreichen, können Sie nach deutschem Recht Ihre Unterkunft stornieren und Ihr Geld zurückverlangen. Wurde die Unterkunft direkt bei Anbietern im Ausland gebucht, kann ausländisches Recht gelten und die rechtliche Situation anders aussehen. (…)

  1. Nur Flug gebucht – Ihre Rechte

Wird ein Flug annulliert, erhalten Sie den Ticketpreis zurück.

Soll ein Flug trotz Einreiseverbot am Zielort stattfinden, können Sie nach der Auffassung der Verbraucherzentrale ebenfalls Ihr Geld zurückverlangen.

Der Flugverkehr ist derzeit stark eingeschränkt. Damit haben Sie in vielen Fällen gute Argumente, den Ticketpreis zurückzuverlangen.

Aktuell wird jedoch angekündigt, dass der Flugverkehr wieder anlaufen soll. Informieren Sie sich regelmäßig über die gebuchte Verbindung.

Fragen Sie bei der Airline nach. Viele Anbieter zeigen sich kulant. Mit einem Gutschein müssen Sie sich bei annullierten Flügen nicht zufriedengeben

https://www.verbraucherzentrale-niedersachsen.de/themen/freizeit-reise/faq-reisen-coronavirus