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

Station registry returns 502 Bad Gateway #6

Closed
bit4gam opened this issue Sep 27, 2022 · 4 comments
Closed

Station registry returns 502 Bad Gateway #6

bit4gam opened this issue Sep 27, 2022 · 4 comments

Comments

@bit4gam
Copy link

bit4gam commented Sep 27, 2022

the station registry fails with following error:

Sep 27 14:00:31 weewx[21] ERROR weewx.restx: StationRegistry: Failed to publish record 2022-09-27 13:56:00 CEST (1664279760): Failed upload after 3 tries

manually registering the station at registry url (http://weewx.com/register/register.cgi) returns following error:

http://weewx.com/register/register.cgi?station_url=https://meteo.fedibox.com/&description=Sapri&latitude=40.074&longitude=15.62&station_type=Ecowitt&station_model=WS2900&weewx_info=4.8.0&python_info=3.9.2&platform_info=Armbian


<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.22.0</center>
</body>
</html>

@bit4gam bit4gam changed the title Station registru returns 502 Bad Gateway Station registry returns 502 Bad Gateway Sep 27, 2022
@tkeffer
Copy link
Contributor

tkeffer commented Sep 27, 2022

It's not just you. It looks like all station registrations are failing with an error similar to:

2022/09/27 15:49:15 [error] 990#0: *1574 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 67.40.252.225, server: weewx.com, request: "GET /register/register.cgi?station_url=https://meteo.fedibox.com/&description=Sapri&latitude=40.074&longitude=15.62&station_type=Ecowitt&station_model=WS2900&weewx_info=4.8.0&python_info=3.9.2&platform_info=Armbian HTTP/1.1", upstream: "fastcgi://unix:/var/run/fcgiwrap.socket", host: "weewx.com"

I'm not sure what's going on. @matthewwall wrote the registration software. I'll have to check with him.

Thanks for calling it to my attention!

@tkeffer
Copy link
Contributor

tkeffer commented Sep 27, 2022

I'll leave it to @matthewwall to fix, but it appears that MySQL is taking a lot of time (8-10 seconds per query) doing queries such as

select max(last_seen) from stations where last_addr='51.38.58.37';

The table stations uses a multi-column index, but rather than be selective about which columns to index, it does them all, in an unfortunate order:

mysql> show indexes from stations;
+----------+------------+----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table    | Non_unique | Key_name       | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+----------+------------+----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| stations |          0 | index_stations |            1 | station_url   | A         |        6959 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| stations |          0 | index_stations |            2 | latitude      | A         |        8047 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| stations |          0 | index_stations |            3 | longitude     | A         |        8583 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| stations |          0 | index_stations |            4 | station_type  | A         |        9196 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| stations |          0 | index_stations |            5 | station_model | A         |       85837 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| stations |          0 | index_stations |            6 | weewx_info    | A         |       85837 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| stations |          0 | index_stations |            7 | python_info   | A         |       85837 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| stations |          0 | index_stations |            8 | platform_info | A         |      128755 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| stations |          0 | index_stations |            9 | last_addr     | A         |      257511 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+----------+------------+----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
9 rows in set (0.65 sec)

The column last_addr is the 9th in the indexing sequence!

I would propose having a two-column index of station_url and last_addr, and dropping all the other indexes.

@tkeffer
Copy link
Contributor

tkeffer commented Sep 27, 2022

I changed the indexes around. First dropped the existing, over-constrained, index:

ALTER TABLE stations DROP INDEX index_stations;

Then created two separate indices:

ALTER TABLE stations ADD UNIQUE INDEX index_url (station_url);
ALTER TABLE stations ADD INDEX index_addr (last_addr);

Performance much improved. The query time for

select max(last_seen) from stations where last_addr='51.38.58.37';

dropped from 10 seconds to a few milliseconds.

After clearing out the backlog of registrations by rebooting, the registration in the original post went through without problem.

Still, I think the registration system needs a rewrite. It's overly complicated, and not very modular. Doing it in a language other than Perl wouldn't hurt either! :-)

@tkeffer tkeffer closed this as completed Sep 27, 2022
tkeffer added a commit that referenced this issue Sep 27, 2022
@tkeffer
Copy link
Contributor

tkeffer commented Sep 27, 2022

Now a lot of time is being spent on queries such as

SELECT * FROM platform_history WHERE datetime=(select max(datetime) FROM platform_history);

So, I added an index for platform_history:

ALTER TABLE platform_history ADD INDEX index_datetime (datetime);

Parenthetically, a better way to construct this query would be:

SELECT * FROM platform_history ORDER BY datetime DESC LIMIT 1;

which uses only one query instead of two.

tkeffer added a commit that referenced this issue Sep 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants