Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cosa può andare storto?

Cosa può andare storto?

I veri problemi saltano fuori sempre dopo essere andati online. Video: https://www.youtube.com/watch?v=XdqktuKwYRU

Riccardo Magliocchetti

June 13, 2014
Tweet

More Decks by Riccardo Magliocchetti

Other Decks in Programming

Transcript

  1. COSA PUO` ANDARE STORTO?
    Riccardo Magliocchetti
    Django Village 2014

    View Slide

  2. whoami
    Web developer @ unbit.is, Torino
    Free software developer
    Maintainer col contagocce di:
    – Bootchart2
    – django-admin-bootstrapped

    View Slide

  3. Quando le cose si spaccano
    Contesto:
    – No rocket science
    – Web Agency vs Startup

    View Slide

  4. Quanti di voi hanno messo online un progetto
    senza dover fare modifiche successivamente?

    View Slide

  5. Configuration Error
    dev devops

    – Application server, db, VM

    View Slide

  6. Trovate il typo
    [uwsgi]
    processes = 1
    threads = 4
    master = true
    check-static = %(basedir)/static
    static-offload-to-thread = 4

    View Slide

  7. Fare la differenza
    -static-offload-to-thread = 4
    +offload-threads = 4
    Nei log:
    uwsgi_response_sendfile_do() TIMEOUT !!!

    View Slide

  8. Logging: lo stile prima di tutto
    plugins = logfile
    logger = staticlogger file:%d/static.log
    log-route = staticlogger app: -1|req: -1
    logto = %d/uwsgi.log
    log-maxsize = 20971520
    log-backupname = %d/uwsgi.log.old

    View Slide

  9. Makes sense!
    ATTENZIONE: spazio disco in esaurimento
    [snip]
    *** Spazio utilizzato: 1.48 GB ***
    *** Spazio ancora disponibile: 16.86 MB ***

    View Slide

  10. WHAT?
    rw-r-----+ 1 foo foo 977M static.log
    rw-r-----+ 1 foo foo 8,9M uwsgi.log
    Lo stile non è poi così importante:
    https://github.com/unbit/uwsgi/issues/542

    View Slide

  11. It's pg-adventure time!
    "psycopg2.OperationalError: FATAL: sorry, too
    many clients already"

    View Slide

  12. That was easy
    --- a/fabric/conf/postgresql/postgresql.conf Tue Mar
    04 19:12:16 2014 +0100
    +++ b/fabric/conf/postgresql/postgresql.conf Tue
    Mar 04 19:36:09 2014 +0100
    @@ -1,5 +1,5 @@
    listen_addresses = ''
    -max_connections = 10
    +max_connections = 20

    View Slide

  13. Django 1.6 to the rescue
    --- a/fabric/conf/django/settings.py Thu Mar 20 16:59:18
    2014 +0100
    +++ b/fabric/conf/django/settings.py Thu Apr 10 10:34:32
    2014 +0200
    @@ -61,6 +61,7 @@
    'PORT': '',
    }
    }
    +CONN_MAX_AGE = 60

    View Slide

  14. Thou shalt deploy
    Rischi: offline & data loss
    Fabric deploy automatizzato

    – Evita lavoro noioso
    – L'errore sta nella ripetizione
    Script fabric sotto controllo di versione (git)

    fabric + git = minimo sindacale

    View Slide

  15. My deploy strategy log
    Copia e incolla da un file di testo

    Copia e incolla da un wiki

    Shell script

    Fabric script

    View Slide

  16. Ad oggi
    Due comandi:

    Setup: fab setup_pgsql bootstrap

    Deploy: fab sync_production
    Stessi comandi su due piattaforme distinte

    View Slide

  17. Lessons learned

    Tutto in git!

    Cdnjs.com

    View Slide

  18. Sometime it's not you
    Ciao Riccardo,
    al posto delle foto vedo solo quadrati bianchi,
    puoi controllare?
    kthxbye,
    Un cliente

    View Slide

  19. Keep calm è andata così
    1. NEGAZIONE: Come sempre è colpa del frontend!
    2. RABBIA: AAAAAAAAAAAAAAAAAAAAAAA
    3. PATTEGGIAMENTO: Dai sarà una stupidaggine
    4. DEPRESSIONE: :(
    5. ACCETTAZIONE: Ci sarà qualcosa nei log:
    *** DANGER cache is FULL !!!

    View Slide

  20. When in doubt use redis
    #velostodicendo
    # configurazione di uwsgi
    redis_pid = %(piddir)/redis.pid
    smart-attach-daemon = %(redis_pid) redis-server --maxmemory
    5mb --maxmemory-policy allkeys-lru --pidfile %(redis_pid)
    --daemonize yes
    # settings.py configurazione di sorl.thumbnail
    THUMBNAIL_KVSTORE =
    "sorl.thumbnail.kvstores.redis_kvstore.KVStore"

    View Slide

  21. Procrastinate it can wait

    Far perdere tempo ai worker
    – Codice lento
    – Fargli fare lavoro che non gli compete

    View Slide

  22. Performance: low hanging fruit
    – se servono alberi usa django-mptt
    – Evita query dentro un loop
    – values() / values_list() / only() sono tuoi amici

    View Slide

  23. UwsgiFouine: come si pronuncia?
    pip install uwsgiFouine
    uwsgiFouine uwsgi.log
    - offline e veloce
    - dati reali
    - conciso, risponde a tre specifiche domande:
    Where was the most time spent?
    What were the slowest pages (max page load time)?
    What were the slowest pages (avg page load time)?

    View Slide

  24. Don't feed the gremlins
    def myview(request):
    send_email(
    “greetings”,
    “hi there!”,
    [email protected]”,
    [“[email protected]”])

    View Slide

  25. Go back in queue!
    Long running tasks:
    – uwsgi spooler
    – python-rq
    – Celery

    View Slide

  26. Dealing with others
    Come se non avessi abbastanza problemi di
    mio ;)

    View Slide

  27. Let's go Enterprise!
    Form Contatti Webservice SOAP

    C = Cliente, R = Riccardo
    C: Alcune richieste che ci mandate restituiscono errore, vi allego docx e pptx con la documentazione
    R: ok, ma avete qualche log che possa aiutarmi?
    C: No!
    R: ok, questo è l'xml che vi mando e questo è quanto tornate voi a me
    256{#} Data has been previously submit ...

    (Il troncamento della stringa di errore non è una correzione editoriale :)
    C: l'xml che ci invii è corretto
    R: …
    C: Torniamo errore se viene mandata la stessa richiesta a meno di 5 minuti di distanza

    View Slide

  28. Enterprise Lessons Learned

    Powerpoint per la documentazione!

    Salvarsi sempre request e response

    Per favore messaggi di errore decenti

    View Slide

  29. How are you doing now?
    pip install uwsgitop
    uwsgi.ini:
    stats: %d/uwsgistats.socket
    uwsgitop uwsgistats.socket
    New Relic, se potete permettervelo

    View Slide

  30. GIVEAWAY

    Chi fa sbaglia

    No log no debug


    KISS please!

    usate uwsgi e redis :)

    No silver bullet

    View Slide

  31. What good amid these O me, O life?
    Answer.
    That you are here—that life exists and identity,
    That the powerful play goes on, and you may
    contribute a verse.
    Walt Whitman – O Me! O Life!
    Leaves of Grass

    View Slide

  32. Grazie
    @rmistaken
    http://github.com/xrmx
    http://github.com/unbit
    http://unbit.is, http://unbit.it

    View Slide